Click here to Skip to main content
15,894,460 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello guys,
I want to know that does C# timers take up new thread or they work on the main thread?
I am having a console app where I have something running or main thread and it takes 4-5 hours. In those 4-5 hours, at every 5 mins I want to run some code. So can I use timer so that it starts new thread and execute while main thread is still running. Or i will have to make thread for timer?
Posted

To be honest, I wouldn't use a timer at all.
Instead, I would set up the code on a BackgroundWorker thread instead, and call Thread.Sleep to pause between runs:
C#
BackgroundWorker work = new BackgroundWorker();
    work.DoWork += new DoWorkEventHandler(work_DoWork);
    work.RunWorkerAsync();
    ...

void work_DoWork(object sender, DoWorkEventArgs e)
    {
    while (true)
        {
        RunYourCodeHere();
        System.Threading.Thread.Sleep(5 * 60 * 1000);  // Wait five minutes
        }
    }
 
Share this answer
 
Comments
Vedat Ozan Oner 18-Mar-14 9:28am    
Why?
OriginalGriff 18-Mar-14 9:50am    
Timers are expensive resources. If you have a long job, then moving it to a background worker makes it run at a lower priority than "normal" tasks, so even when it does run it has little or no effect on your user - because all his other tasks (inside or outside your application) are running at normal priority and thus "bump" the background worker and run ahead of it.
Not only do timers consume system resources, but they can also generate problems: what happens if you get a timer tick but you haven't completed the previous run yet? If you use a "pause" within the thread that can't happen.

Have a look with google, and you will see there are quite a lot of people who advocate that timers are evil and should never be used!

Don't get me wrong - Timers are very useful, and I use them quite often. But there are times when you are better off not using them.
Vedat Ozan Oner 18-Mar-14 16:54pm    
thanks for the explanation. I haven't ever used (thought) backgroundworker as in your sample. this is very useful. but what happens if "RunYourCodeHere" method takes a long time compared to "thread.sleep"? I think the aim determines which technique is better to use.
OriginalGriff 18-Mar-14 17:05pm    
Doesn't matter - the thread won't sleep until the RunYourCodeHere returns anyway.
Vedat Ozan Oner 18-Mar-14 17:45pm    
yes right but what I try to say is that, for example, I want to run "RunYourCodeHere" for (about) every 5 minutes. but let's say "RunYourCodeHere" takes time between 1-3 minutes. then it runs every 6-8 minutes, not 5 minutes.
From msdn documentation [^]-
"System.Threading.Timer is a simple, lightweight timer that uses callback methods and is served by thread pool threads. It is not recommended for use with Windows Forms, because its callbacks do not occur on the user interface thread. System.Windows.Forms.Timer is a better choice for use with Windows Forms. For server-based timer functionality, you might consider using System.Timers.Timer, which raises events and has additional features."

Also, check out this answer[^].
 
Share this answer
 
Comments
Ami_Modi 18-Mar-14 8:35am    
So I think I dont require to thread timer as it is served by thread pool thread. Am I right?

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