473,387 Members | 1,483 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Timer not firing correctly - strange problem....

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
2 4511
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Al Newton | last post by:
I created a DLL that reads a pointer to a vector created in a console-mode test app as: vector<LineItem*>* pLineItems = new vector<LineItem*>; Everything seems to work fine (except for memory...
0
by: zishen | last post by:
Hello, recently I came across a strange problem with my app. I utilities interop to make DirectShow into .NET. When I use it in single thread, it works fine. But when I use System.Threading.Timer...
3
by: Sameh Halabi | last post by:
Hi I have a strange problem in C , I hope someone may help with it . the problem is as follows : I have an existing big structure (that I can't change) which contains in it groups of...
4
by: Gav | last post by:
Hi all, I'm having this strange problem where, in a web app, I have 2 different links to a different form. One is just a straight forward link the other is a bit more complicated because it gets...
1
by: Sam Kong | last post by:
Hello! Recently I had a strange problem with Visual C# 2005 beta 1. When I ran(F5 key) a program, <#if DEBUG> statement was not working. It ran as RELEASE mode. So I had to manually define...
0
by: Mark | last post by:
Hi - I have a really strange problem - straight forward login code (on the event of a button press). Works perfectly locally - but when I upload to my host, I get the message and stack shown...
0
by: Mark | last post by:
I have a very strange problem that is driving me nuts. My application is using the webclient object to send data to a webserver running on a unix box. We use 2 different environments, 1 for...
7
by: E | last post by:
I have encountered a very strange problem that is hard to describe, but hope someone has run across it. I have a form that shows a number of columns from a single table in the upper portion of...
12
by: ishtar2020 | last post by:
Hi everybody I've been writing my very first application in Python and everything is running smoothly, except for a strange problem that pops up every once in a while. I'm sure is the kind of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.