By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,758 Members | 1,264 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,758 IT Pros & Developers. It's quick & easy.

Timer not firing correctly - strange problem....

P: n/a
I've got a process I want to run in a thread separate from my main
application thread, so I've used a backgroundworker component, and in
frmMain.Load I invoke the code using Backgroundworker1.RunWorkerAsync().

So far so good, this works fine. But when the process is done, I want to
wait 30 minutes and then run the process again. So I added a
System.Windows.Forms.Timer component to the form and in the
Backgroundworker1_RunWorkerCompleted event, I start the timer like this:

Timer1.Interval = 3600000; //3600000 ms = 60 minutes
Timer1.Enabled = true;

in the Timer1_Tick event, I simply start the backgroundworker again using
Backgroundworker1.RunWorkerAsync()

I would expect the timer to run for an hour and then restart the
backgroundworker, but what really happens is that the Timer1_Tick event
fires after a very short time, between 20 and 30 seconds (and not the same
interval every time; The first time it happens, it can be 22 seconds, the
next 28 seconds etc. - no pattern).

WHY IS THAT? I cannot for the life of me explain what happens. First I
thought there was a problem with the Timer component, so I tried to create e
new timer instance every time I got to the
Backgroundworker1_RunWorkerCompleted event, but that didn't help - same
thing happened.

Then I thought: Well, maybe it has something to do with the fact that the
background thread disposes after a short interval when the process is over,
but even if that's the case, it ought not interfere with the timer since
code in the Backgroundworker1_RunWorkerCompleted event (as far as I know) is
executed in the main application thread, and the timer itself is created in
the main applcation thread. And just because it fires prematurely, the
component still exists - it's not disposed of.

Has anybody got an explanation, and what can I do to avoid the problem?

TIA,
Johnny J.
Oct 16 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Thu, 16 Oct 2008 12:13:26 -0700, Johnny J├Ârgensen <jo**@altcom.se>
wrote:
[...]
Timer1.Interval = 3600000; //3600000 ms = 60 minutes
Timer1.Enabled = true;

in the Timer1_Tick event, I simply start the backgroundworker again using
Backgroundworker1.RunWorkerAsync()

I would expect the timer to run for an hour and then restart the
backgroundworker, but what really happens is that the Timer1_Tick event
fires after a very short time, between 20 and 30 seconds (and not the
same
interval every time; The first time it happens, it can be 22 seconds, the
next 28 seconds etc. - no pattern).

WHY IS THAT? I cannot for the life of me explain what happens. [...]
Well, the code you posted is fine. So your bug is in the code you didn't
post.

Without a concise-but-complete code example that reliably demonstrates the
problem, it's not possible to explain what's going on.

If I had to guess (well, okay...I do have to guess), I'd say that you
aren't disabling the Timer when the Tick event is raised. If a Timer is
already started, starting it again has no effect...it will continue firing
on the previous schedule. But, this would only really be a significant
problem if you were creating new timers, and your post implies that you
get the "too soon" behavior even when you're reusing the previous timer.
So that guess isn't really consistent with what you've written.

If you want a good answer, you have to post a better question. :)

Pete
Oct 16 '08 #2

P: n/a
Just a suggestion but have you tried using a System.Timers.Timer. The
difference is that the Elasped event is raised on a threadpool thread so you
wouldnt have to control the async behaviour yourself. You could set the timer
originally with an interval of 100, then on elasped, stop the timer, do the
work in the same thread, the set the interval to 360000 and restart the
timer.
If you were using a Windows.Forms.Timer to enable you to update the UI, you
can use Control.Invoke to do that from the background thread the timer uses.
This is how I would have approached it and I believe I have done this in the
past with no issues.
--
Ciaran O''Donnell
http://wannabedeveloper.spaces.live.com
"Johnny J├Ârgensen" wrote:
I've got a process I want to run in a thread separate from my main
application thread, so I've used a backgroundworker component, and in
frmMain.Load I invoke the code using Backgroundworker1.RunWorkerAsync().

So far so good, this works fine. But when the process is done, I want to
wait 30 minutes and then run the process again. So I added a
System.Windows.Forms.Timer component to the form and in the
Backgroundworker1_RunWorkerCompleted event, I start the timer like this:

Timer1.Interval = 3600000; //3600000 ms = 60 minutes
Timer1.Enabled = true;

in the Timer1_Tick event, I simply start the backgroundworker again using
Backgroundworker1.RunWorkerAsync()

I would expect the timer to run for an hour and then restart the
backgroundworker, but what really happens is that the Timer1_Tick event
fires after a very short time, between 20 and 30 seconds (and not the same
interval every time; The first time it happens, it can be 22 seconds, the
next 28 seconds etc. - no pattern).

WHY IS THAT? I cannot for the life of me explain what happens. First I
thought there was a problem with the Timer component, so I tried to create e
new timer instance every time I got to the
Backgroundworker1_RunWorkerCompleted event, but that didn't help - same
thing happened.

Then I thought: Well, maybe it has something to do with the fact that the
background thread disposes after a short interval when the process is over,
but even if that's the case, it ought not interfere with the timer since
code in the Backgroundworker1_RunWorkerCompleted event (as far as I know) is
executed in the main application thread, and the timer itself is created in
the main applcation thread. And just because it fires prematurely, the
component still exists - it's not disposed of.

Has anybody got an explanation, and what can I do to avoid the problem?

TIA,
Johnny J.
Oct 17 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.