Click here to Skip to main content
15,917,795 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi all,

I wanted to how long the timers in windows form run once they are triggered by an event. Since i have developed an app which has to run continuously, until user stops it. Most likely it should never stop once started. So will the timers run continuously for 24hrs a day & 365 days...?
Posted
Comments
Sergey Alexandrovich Kryukov 14-Mar-13 1:47am    
Why? What timer do you use? There are at least 4 of them.
—SA
Steve44 15-Mar-13 3:15am    
What you describe sounds like you want to run some longer executing task in the timer, which is not a good design. For a longer running task you should spin up a separate thread which can be triggered by a timer, but runs independent of it.
If this is not what you intended, please explain in more detail.
Jagadisha_Ingenious 16-Mar-13 4:32am    
@Sergy Alexandrovich Kryukov:I am using system timer for getting & windows form timer for updating UI. Which timer among the four available can do the work as told earlier?
Sergey Alexandrovich Kryukov 18-Mar-13 1:38am    
It depends on your application. In almost all cases, it's better to use threading. Most usually used timer is System.Timers.Timer.
If you need regular time intervals (such as in animation), never ever use System.Windows.Forms.Timer.

"The work as told"? The problem is: nothing is told. And you still did not answer what timer did you use.
—SA
Jagadisha_Ingenious 18-Mar-13 10:20am    
@Sergey Alexandrovich Kryukov: Hi, i am using system.threading.timer in my application. I have used one of them for receiving data from serial port & other timer making button change its back color continuously indicating fault as per my application. So do these timers perform as expected, run continuously?

1 solution

No, there is no such thing as "time overflow". It will continue to work with unlimited number of events.

[EDIT]

Please see my comments to this answer. You cannot do what you are trying to do.
Please see my past answer to get a better idea: Polling Database with Timer[^].

—SA
 
Share this answer
 
v2
Comments
Jagadisha_Ingenious 25-Mar-13 0:46am    
Then why does the timer stop when running? Is it because of the UI i have developed? Sometimes even the system hangs when the timer is running, why is that so...?
Sergey Alexandrovich Kryukov 25-Mar-13 0:57am    
It shouldn't. Well, can you make a code sample to manifest this problem? Only short and complete, focused on just one problem. It sounds strange, maybe you did something weird...
—SA
Jagadisha_Ingenious 25-Mar-13 1:13am    
private void timer2_Tick(object sender, EventArgs e)
{
for (int n = 0; n < btn.Length; n++)
{
if (errorIndexOne[n] == 1)
{
btnTest.BackgroundImage = global::AnnunciatorMonitoringTool.Properties.Resources.lightbulb;
string str = "insert into ErrorLog values('" + annName + "','" + btn[n].Text + "','" + DateTime.Now.ToShortDateString() + "','" + DateTime.Now.ToShortTimeString() + "')"; ;
DataBaseHandling(str);
if (btn[n].BackColor == Color.LightGreen)
{
btn[n].BackColor = Color.Red;
}
else
{
btn[n].BackColor = Color.LightGreen;
}
}

else if (errorIndexOne[n] != 1)
{
btn[n].BackColor = Color.LightGreen;
}

}

I am trying to record each flashing event of the button in the database. The UI consists of few buttons in a panel & the panel is surrounded by rectangle making it look like a border around the panel. The panel size is changeable based on the no of buttons in it.
This timer2_tick event is fired when a particular data is coming from serial port. I am looking for a particular data from serial port if get that data i fire this timer event & further process the rest.
Sergey Alexandrovich Kryukov 25-Mar-13 1:17am    
You cannot do such things. First, a timer cannot guarantee execution in UI thread. You handler does not guarantee that it is complete soon enough. Just imagine what can happen if a new timer event is invoked before it is complete... and a lot more problem. I'll give you one good idea on better approach.
—SA
Jagadisha_Ingenious 25-Mar-13 1:18am    
Sure & thanks u sir..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900