Using global timer to prevent SuspendMode

  • Thread starter Thread starter Peter B
  • Start date Start date
P

Peter B

Hello everyone!

Up until now I have used timers in my forms, where I read and write data to
file from the database, to prevent the auto-suspend mode to happen. The
timer calls SystemIdleTimerReset(); every tick (about 28s).

This has been working fine but since I need the functionality from different
places I thought it would be nice to use a Global static method that started
and stopped a global timer with the same time intervall instead of declaring
timers in each form.

The problem is that when a processor intensive operation is involved the
timer seem to lag several seconds... why is this? How come this didn't
happen when the timers where located in the forms? Is it possible to prevent
this? Should I just lower the timer intervall to e.g. 10s? What happens when
even longer processor intensive operations occurres?

Any help is appreciated!

/ P
 
It's because timers are, effectively, lower priority than whatever processor
intensive stuff you are doing. They just send messages to the main
application message loop, when they fire, and the loop has to be getting
processor time in order for them to do anything.

Paul T.
 
Hello Paul!

It seems to work when the timer is in the current active form compared to
when the timer is in a global class. Is there a good explanation for this,
or have I just misinterpreted my results?

/ Peter
 
It's probably related to who is running the message loop. Are you using
System.Threading.Timer in that case? If you have some sort of lengthy
process you need to run, that should be done in a thread, not in the UI
thread of the application. System.Threading.Timer does this, best I can
tell. You could also write it yourself using a thread that you create and
some system calls. You can use WaitForSingleObject() to provide both a
means to notify the thread to exit and a much more accurate timebase. Your
code would look something like this:

in the thread routine:

while ( 1 )
{
result = WaitForSingleObject( exitEvent, periodForProcess );
if ( result == OBJECT_0 )
break; // When the event is set, this means that it's time to
leave.

// The other case is a time-out, so it's time to do our lengthy process.
// .... do process here.
}

in the main routine:

// When it's time to exit the application or when you no longer want to have
// the thread running, call SetEvent() to set the exitEvent to the signaled
state.
// The thread will see this the next time it finishes its lengthy process
and
// exit.

// You can wait on the thread's thread handle after setting the event to
make sure
// that the thread has exited before you terminate the application. This
assures a
// cleaner exit.
WaitForSingleObject( threadHandle, waitTimeout );

Paul T.
 
I thought the timer was operating in a separate thread?

Also, what is the difference between System.Threading.Timer and
System.Windows.Forms.Timer?

/ Peter
 
Back
Top