tags:

views:

85

answers:

2

Sorry if this is a newbie question.

I have a VB.NET subroutine that is triggered by a timer. I have found that on occasion, if the routine runs for a long time, the routine will be triggered again while it is still running with unpredictable results. Is this normal? Is each copy of the routine running in its own thread? Is there a way to serialize the routines? Right now I use a Boolean that I set True on entry and False when I am done so I can check if the routine is running when I enter it, but I thought that there might be something more elegant.

+2  A: 

Stop the timer when you start executing your subroutine, and start it again on exit.

You should probably include error handling such that the timer is restarted even on an unexpected exit.

Mitch Wheat
+1  A: 

A System.Windows.Forms.Timer is single threaded, so I'm surprised that you'd have the Tick event being called while it is already running (by definition, the UI thread should be occupied, and unable to make the call)

I could see the Tick events queuing up, and happening sequentially...

A System.Timers.Timer is multi-threaded, and could have the situation you describe. (If you're using one of those, switching to a System.Windows.Forms.Timer will at least keep the events from interrupting each other)

Daniel LeCheminant