473,657 Members | 2,397 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 backgroundworke r component, and in
frmMain.Load I invoke the code using Backgroundworke r1.RunWorkerAsy nc().

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
Backgroundworke r1_RunWorkerCom pleted 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 backgroundworke r again using
Backgroundworke r1.RunWorkerAsy nc()

I would expect the timer to run for an hour and then restart the
backgroundworke r, 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
Backgroundworke r1_RunWorkerCom pleted 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 Backgroundworke r1_RunWorkerCom pleted 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 4535
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 backgroundworke r again using
Backgroundworke r1.RunWorkerAsy nc()

I would expect the timer to run for an hour and then restart the
backgroundworke r, 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.T imer. 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.T imer 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 backgroundworke r component, and in
frmMain.Load I invoke the code using Backgroundworke r1.RunWorkerAsy nc().

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
Backgroundworke r1_RunWorkerCom pleted 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 backgroundworke r again using
Backgroundworke r1.RunWorkerAsy nc()

I would expect the timer to run for an hour and then restart the
backgroundworke r, 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
Backgroundworke r1_RunWorkerCom pleted 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 Backgroundworke r1_RunWorkerCom pleted 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
1598
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 leaks) if I don't do a delete in the test app. But if I do, I get an exception on the test app's return 0 statement. BoundsChecker tells me that I am attempting to free memory in the DLL that was allocated in the test routine. I don't understand...
0
891
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 to trigger some actions with DirectShow, there will occur some InvalidCastExceptions at QueryInterface(). However, I change timer to System.Windows.Form.Timer, this problem disappears. Anyone got this problem before? And anyone who knows why it...
3
2062
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 variables that is repeated . typedef struct big_struct { ..... ..... char a1;
4
1607
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 there from a user control I wrote to display a menu. However both call the page in the same way using window.open('page.aspx','_top'); The problem is that:.... The page it calls, in the page load, checks to see if a session variable is null,...
1
3761
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 DEBUG to make #if DEBUG work. When I first started the project, this problem didn't exist. The problem started to exist in the middle of doing the project. Today, I decided to figure out what went wrong.
0
1038
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 below. If I take out the 'Handles btnLogin.Click' - then it doesn't fire the btnClick routine - BUT - the page will display on my host (albeit it won't do anything when I press the button)? I'd really appreciate some help with this.
0
1430
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 development and 1 for a staging environment. I'm using an invalid servlet name in the url deliberately in order to test my logic for basic communication problems. When I run the code against the staging box, I get a 5xx error back, which is what I...
7
1385
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 the form in a datasheet layout. The user can then choose a category from a dropdown list, and based on that category will see further data in the bottom part of the screen. They can choose one of 2 categories, but both show data FROM THE SAME...
12
1499
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 newbie thing every seasoned programmer knows. Sometimes a receive strange Syntax Errors from parts of code that worked perfectly minutes ago. What's even more puzzling is that those errors are pointed to another part of the module when I do some...
0
8402
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8829
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8734
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8508
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8608
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6172
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
2733
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1962
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1627
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.