Re: How do I do a delay?



Sergei Naumov <serge@astro.unc.edu> writes:

| I need to slow down my output, so I could have a control over the delay
| time, and I was thinking about the best way to achieve this. I could use
| gtk_timeout_add() that executes a function once in a certain period of time
| but it is not quite like what I want and implementing a delay like that
| would be clumsy, wouldn't it? Does anyone have any idea on how I can
| achieve this?

sleep or usleep

If you do not want to use usleep, you can emulate it with

select(1, NULL, NULL, NULL, your_time_struct)

This example waits 1 second and writes on byte to stdout:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

void main(int argc, char *argv[])
{
  char one = 1;
  struct timeval tv;
  int retval;
  int arg_sec = 1;
  int arg_usec = 0;

  if (argc != 3) {
    fprintf (stderr, "Usage: utimer <seconds> <microseconds>\n");
    exit (1);
  }

  while (1) {
    tv.tv_sec = arg_sec;
    tv.tv_usec = arg_usec;
    retval = select (1, NULL, NULL, NULL, &tv);
    if (retval) {
      fprintf (stderr, "%s: *** error\n", argv[0]);
      exit (1);
    } else {
      write (STDOUT_FILENO, &one, 1);
    }
  }
}

But useing sleep for a Gtk program is the wrong way, because the gui
can not be updated.

-- 
http://www.ping.de/sites/aibon/



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]