Run thread 20 minutes after every run

Viewed 154

Hi i have this c# windows form application and i have this thread under the InitializeComponent()

now it runs continuously after application start now i want is to run that every 20 minutes after a run for example

run
Wait 20 m.
run 
wait 20 m.
run
.... 

here's the code

Thread thread = new Thread(() =>
                {
                    while (true)
                    {

                       //some process

                    }
                });
                thread.IsBackground = true;
                thread.Start();

I remove the code inside.

what i'm facing is the application is taking too much of the computer network beacause the thread runs a query every milisec so i need only to run the thread every 20minutes after every runs Hope i make my explanation clear

Thankyou in advance.

1 Answers

You can use Task.Delay and asynchronous lambda:

Thread thread = new Thread(async () =>
            {
                while (true)
                {
                    await Task.Delay(TimeSpan.FromMinutes(20));
                    //some process
                    Console.WriteLine("1");

                }
            });

Or use one of .NET Timer's to execute method on specified intervals.

Related