473,564 Members | 2,758 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Threading.Timer fires many times??

Hi, All
Recently, my project need some code like following:

using System;
using System.Threadin g;

namespace MyTimerTest
{
class Class1
{
protected System.Threadin g.Timer guarder;
private string testFlag="";

public void Init()
{
System.Console. WriteLine("Begi n Test.....");
guarder=new Timer(new
TimerCallback(C heckCurrentStat e),null,5000,10 0);
}

public void Destroy()
{
//Dispose timer
}
protected void CheckCurrentSta te(object state)
{
string
strTemp=System. Threading.Threa d.CurrentThread .GetHashCode(). ToString();
try
{
if(testFlag!="" && testFlag!=strTe mp)
{
System.Console. WriteLine("Fata l error1: "+testFlag+","+ strTemp);
}

testFlag=strTem p;

guarder.Change( Timeout.Infinit e,0); //disable the timer

System.Threadin g.Thread.Sleep( 300);
}
catch(Exception ex)
{
System.Console. WriteLine("Chec kCurrentState: "+ex.Messag e);
}
finally
{
if(testFlag!=st rTemp)
{
System.Console. WriteLine("Fata l error2: "+testFlag+","+ strTemp);
}
testFlag="";

guarder.Change( 100,100); //enable timer again
}
}

[STAThread]
static void Main(string[] args)
{
Class1 dd=new Class1();

dd.Init();

System.Console. ReadLine();

dd.Destroy();
}
}
}

I confused that when I run the simple test code for a moment, i will
get the following output
Fatal error1: 1,2
Fatal error2 ,2
......

I think when it enters the event process it has disable the timer using

Change(Timeout. Infinite,0)

According to the output, the timer fires multi-times, why?

Thanks for any ideas
Harry

Nov 17 '05 #1
6 6125
g> I think when it enters the event process it has disable the timer using

Change(Timeout. Infinite,0)

According to the output, the timer fires multi-times, why?
guarder.Change( 100,100); //enable timer again

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
A watched clock never boils.

<wh*****@gmail. com> wrote in message
news:11******** **************@ g47g2000cwa.goo glegroups.com.. . Hi, All
Recently, my project need some code like following:

using System;
using System.Threadin g;

namespace MyTimerTest
{
class Class1
{
protected System.Threadin g.Timer guarder;
private string testFlag="";

public void Init()
{
System.Console. WriteLine("Begi n Test.....");
guarder=new Timer(new
TimerCallback(C heckCurrentStat e),null,5000,10 0);
}

public void Destroy()
{
//Dispose timer
}
protected void CheckCurrentSta te(object state)
{
string
strTemp=System. Threading.Threa d.CurrentThread .GetHashCode(). ToString();
try
{
if(testFlag!="" && testFlag!=strTe mp)
{
System.Console. WriteLine("Fata l error1: "+testFlag+","+ strTemp);
}

testFlag=strTem p;

guarder.Change( Timeout.Infinit e,0); //disable the timer

System.Threadin g.Thread.Sleep( 300);
}
catch(Exception ex)
{
System.Console. WriteLine("Chec kCurrentState: "+ex.Messag e);
}
finally
{
if(testFlag!=st rTemp)
{
System.Console. WriteLine("Fata l error2: "+testFlag+","+ strTemp);
}
testFlag="";

guarder.Change( 100,100); //enable timer again
}
}

[STAThread]
static void Main(string[] args)
{
Class1 dd=new Class1();

dd.Init();

System.Console. ReadLine();

dd.Destroy();
}
}
}

I confused that when I run the simple test code for a moment, i will
get the following output
Fatal error1: 1,2
Fatal error2 ,2
.....

I think when it enters the event process it has disable the timer using

Change(Timeout. Infinite,0)

According to the output, the timer fires multi-times, why?

Thanks for any ideas
Harry

Nov 17 '05 #2
Thanks Kevin
but would you mind giving me more detailed tips, i do not understand
what your said completely :-)

regards
Harry

Nov 17 '05 #3
wh*****@gmail.c om wrote:
Thanks Kevin
but would you mind giving me more detailed tips, i do not understand
what your said completely :-)

regards
Harry


What he said was, that the timer is firing many times because you turn
the timer on again with the line of code that he pointed out.

try removing that line of code so that the timer only executes once.
Nov 17 '05 #4
Hi jeremiah
Maybe i did not describe my problem clearly :-)
I want to execute some tasks repeatedly
There should be some interval between two executions.
If one execution consumes too many time, Threading.Timer will use a new
thread to execute
callback function before previous execution completed, it result in
multi-threads execute these tasks at the same time
but i want to just only one thread do it, so before one execution begin
i disable
the Timer, and after it completed, i enable the timer again.
Yes, i can use other ways to avoid the problem, but i want to know why
my test code failed?

wish to your reply :-)

regards
Harry

Nov 17 '05 #5
wh*****@gmail.c om wrote:
guarder=new Timer(new
TimerCallback(C heckCurrentStat e),null,5000,10 0);


Unless I have mis-understood how the timer works, your statement means:
"After 5000ms has passed, call the CheckCurrentSta te callback and then
call it every 100ms thereafter".

Is that your intent? To call CheckCurrentSta te after 5 seconds and
then every 10th of a second thereafter? Or do you want it called every
5 seconds? To do that you would use guarder = new Timer(new
TimerCallback(C heckCurrentStat e), null, 5000, 5000);

Perhaps, because the timers interval is 100ms, that it is being called
more than once before it is disabled? What if you moved the line to
disable the timer to the top of the callback method so it gets disabled
as soon as possible?

Nov 17 '05 #6

Chris Dunaway 写道:
wh*****@gmail.c om wrote:
guarder=new Timer(new
TimerCallback(C heckCurrentStat e),null,5000,10 0);


Unless I have mis-understood how the timer works, your statement means:
"After 5000ms has passed, call the CheckCurrentSta te callback and then
call it every 100ms thereafter".

Is that your intent? To call CheckCurrentSta te after 5 seconds and
then every 10th of a second thereafter? Or do you want it called every
5 seconds? To do that you would use guarder = new Timer(new
TimerCallback(C heckCurrentStat e), null, 5000, 5000);

Perhaps, because the timers interval is 100ms, that it is being called
more than once before it is disabled? What if you moved the line to
disable the timer to the top of the callback method so it gets disabled
as soon as possible?


Thanks Chris
i do not understand why the disable operation will consume more than
100ms? The GC is working?

regards
Harry

Nov 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1159
by: Daniel Maycock via .NET 247 | last post by:
I can't get my threading timer to show a splash screen panel for six seconds, then move onto the next panel. The timer just doesn't tick (this is aparent when I set the time to wait to 100 and it never fires off the sub) I've tried using java, I've tried using the system timer - this is my last hope - please help! Here's my Code...
3
5578
by: ELO | last post by:
Hi all Every week, I need to get two files on a remote server. I have developped a C# Windows Service with two System.Threading.Timer to do this task For the first one, the delay (TimeSpan dueTime) is always set to 6 days, 23 hours, 59 minutes, .. Some weeks ?!?, the timer restarts immediately after its execution (and loop indefinitely)....
6
2846
by: Dan | last post by:
I've created a pocketpc app which has a startup form containing a listview. The form creates an object which in turn creates a System.Threading.Timer. It keeps track of the Timer state using a TimerState object similar to the example in the System.Threading.Timer documentation. The method which handles the timer events, among other things,...
3
2312
by: Kenny | last post by:
I am running a windows service that takes actions based on a couple System.Threading.Timers. The intervals are usually short... based on the time of day, anywhere between 1 and 5 minutes. Recently however, the event that was firing based on the timer started firing rapidly; it fired about 9000 times in a minute and a half. After the minute...
1
3281
by: Paul Tomlinson | last post by:
Question about a System.Threading.Timer object and the "state" object you pass to it... Timer stateTimer = new Timer( = new TimerCallback( OnTimer ), o, 1000, 1000); I have an array of timer objects which all fire into my OnTimer( object state ) function very nicely. I pass in an object "o" on creation of this timer which I subsequently...
9
7255
by: HL | last post by:
I am using VS 2005 Beta - C# Problem: The Timer fires a few milliseconds before the actual Due-Time Let's say a timer is created in the following manner: System.Threading.Timer m_timer = null; Let's declare a constant Int32 m_TimePeriod = 10000;
3
5957
by: mjheitland | last post by:
Hi, I like to know how many threads are used by a Threading.Timer object. When I create a Threading.Timer object calling a short running method every 5 seconds I expected to have one additional ThreadPool thread. And that is exactly what MS VIsual Studio shows. But when I run Processexplorer or Taskmanager I see 2 additional threads,...
3
1764
by: Daniel Maycock via .NET 247 | last post by:
I can't get my threading timer to show a splash screen panel for six seconds, then move onto the next panel. The timer just doesn't tick (this is aparent when I set the time to wait to 100 and it never fires off the sub) I've tried using java, I've tried using the system timer - this is my last hope - please help! Here's my Code...
2
2850
by: KSC | last post by:
Hello, I have used a thread timer as in the documentation on MSDN in my VB.NET application. Using System.Threading.Interlocked.Increment I increment the counter to a certain point, perform an operation, then dispose of the TimerReference. In this case, the operation is simply enabling a checkbox control. I have found that the timer...
0
7583
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7888
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. ...
0
8106
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...
1
7642
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...
0
7950
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...
0
6255
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
2082
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
1
1200
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
924
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...

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.