How to handle repetitive execution?

I am trying to verify the correctness of the solution, which I consider to be a fairly typical problem. I have a service, and every 10 minutes he has to do something. I ended up with the following:

private AutoResetEvent autoResetEvent = new AutoResetEvent(true);
private bool isRunning = true;

public void Execute()
{
    while(isRunning)
    {
       DoSomething();

       if(isRunning)
       {
         autoResetEvent.WaitOne(new Timespan(0, 10, 0));
       }
    }
}

public void Stop()
{
    isRunning = false;
    autoResetEvent.Set();
}

The immediate potential problems that I see is that I do not make any locks in the isRunning modification in Stop (), which is called by another thread, but I'm not sure what I really need? The worst that I think can happen is that it starts one extra loop.

Also, are there any obvious issues with this code? Is there a better way to solve this problem that I don't know about?

+3
source share
4 answers

System.Threading.Timer:

Timer tmr = new Timer(DoSomething, null, new TimeSpan(0, 0, 0), new TimeSpan(0, 10, 0))
+7

10 ; 10 , DoSomething - . 10 DoSomething.

, Quartz.Net.

+1

- Windows Server AppFabric, , . , . , , , .

0

Windows Workflow Foundation? , .

You can run appruntime through your program, and then let the WWF application take care of things.

0
source

Source: https://habr.com/ru/post/1745473/


All Articles