Click here to Skip to main content
14,300,549 members
Rate this:
Please Sign up or sign in to vote.
See more:
Best way to call method every second in windows service


Advanced Thanks !!!

What I have tried:

<pre>
//This one triggering the MyMethod(); 8 times after that it not triggering MyMethod();
var startTimeSpan = TimeSpan.Zero;
var periodTimeSpan = TimeSpan.FromSeconds(1);

var timer = new System.Threading.Timer((e) =>
{
    MyMethod();   
}, null, startTimeSpan, periodTimeSpan);
Posted
Updated 11-Sep-19 1:15am
v4
Comments
CHill60 24-Jan-18 5:21am
   
What is wrong with what you have? (other than you are going to be tying up CPU)
Rate this:
Please Sign up or sign in to vote.

Solution 1

I usually use :
System.Timers.Timer timer = new System.Timers.Timer(1000);
timer.AutoReset = true; // the key is here so it repeats
timer.Elapsed += timer_elapsed;
timer.Start();
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

Task task = new Task(() =>
                {
                    while (true)
                    {
                        MyMethod();
                        Thread.Sleep(1000);
                    }
                });
                task.Start();
   
v2

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100