Re: Recalculation of timeouts with g_timeout_add



Let your callback return FALSE and reregister before the return

#include <glib.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

gboolean callback (gpointer data) {

  int seconds = 0;
  struct tm *mytm;
  time_t mytime;

  mytime = time( NULL );
  mytm = gmtime( &mytime );
  
  printf("Called :");   
  printf( "%d-%2.2d-%2.2d ", mytm->tm_year+1900, 
mytm->tm_mon+1,mytm->tm_mday); 
  printf( "%2.2d:%2.2d:%2.2d\n", mytm->tm_hour, 
mytm->tm_min,mytm->tm_sec); 

  // Sleeping som random time
  seconds = random()%5+1;
  sleep(seconds);
  
  g_timeout_add (10*1000, callback, NULL );

  mytime = time( NULL );
  mytm = gmtime( &mytime );

  printf("Return :");   
  printf( "%d-%2.2d-%2.2d ", mytm->tm_year+1900, 
mytm->tm_mon+1,mytm->tm_mday); 
  printf( "%2.2d:%2.2d:%2.2d\n", mytm->tm_hour, 
mytm->tm_min,mytm->tm_sec); 
  
  //printf ("callback returns at: %lu\n", time (NULL));
  return FALSE;
  
}


int main( int argc, char ** argv )
{

  GMainLoop *loop;
  loop = g_main_new( TRUE );
  g_timeout_add (10*1000, callback, NULL )
  g_main_run( loop );
  return(0);
}



-- 
Göran Hasse

----------------------------------------------------------------
Göran Hasse           email: gorhas raditex se  Tel: 019-450105
Raditex AB             http://www.raditex.se    
Planiavägen 15, 1tr                             Mob: 070-5530148
131 34  NACKA, SWEDEN                         OrgNr: 556240-0589
VAT: SE556240058901
------------------------------------------------------------------




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