473,499 Members | 1,922 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.Threading;

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

public void Init()
{
System.Console.WriteLine("Begin Test.....");
guarder=new Timer(new
TimerCallback(CheckCurrentState),null,5000,100);
}

public void Destroy()
{
//Dispose timer
}
protected void CheckCurrentState(object state)
{
string
strTemp=System.Threading.Thread.CurrentThread.GetH ashCode().ToString();
try
{
if(testFlag!="" && testFlag!=strTemp)
{
System.Console.WriteLine("Fatal error1: "+testFlag+","+strTemp);
}

testFlag=strTemp;

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

System.Threading.Thread.Sleep(300);
}
catch(Exception ex)
{
System.Console.WriteLine("CheckCurrentState: "+ex.Message);
}
finally
{
if(testFlag!=strTemp)
{
System.Console.WriteLine("Fatal 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 6115
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.googlegr oups.com... Hi, All
Recently, my project need some code like following:

using System;
using System.Threading;

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

public void Init()
{
System.Console.WriteLine("Begin Test.....");
guarder=new Timer(new
TimerCallback(CheckCurrentState),null,5000,100);
}

public void Destroy()
{
//Dispose timer
}
protected void CheckCurrentState(object state)
{
string
strTemp=System.Threading.Thread.CurrentThread.GetH ashCode().ToString();
try
{
if(testFlag!="" && testFlag!=strTemp)
{
System.Console.WriteLine("Fatal error1: "+testFlag+","+strTemp);
}

testFlag=strTemp;

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

System.Threading.Thread.Sleep(300);
}
catch(Exception ex)
{
System.Console.WriteLine("CheckCurrentState: "+ex.Message);
}
finally
{
if(testFlag!=strTemp)
{
System.Console.WriteLine("Fatal 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.com 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.com wrote:
guarder=new Timer(new
TimerCallback(CheckCurrentState),null,5000,100);


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

Is that your intent? To call CheckCurrentState 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(CheckCurrentState), 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.com wrote:
guarder=new Timer(new
TimerCallback(CheckCurrentState),null,5000,100);


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

Is that your intent? To call CheckCurrentState 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(CheckCurrentState), 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
1152
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...
3
5559
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...
6
2836
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...
3
2302
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. ...
1
3271
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...
9
7243
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;...
3
5954
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...
3
1761
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...
2
2846
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...
0
7007
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
7174
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,...
0
7220
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...
0
7388
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...
0
5470
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,...
1
4919
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...
0
4600
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3091
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1427
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 ...

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.