Skip to main content
 首页 » 编程设计

c#之任务同步的正确方法

2024年02月05日19zhwl

我正在做的事情是否是完成此任务的正确/最佳方法?

我有一个带计时器的窗口。每次计时器计时时,我都会调用如下所示的 RunTask 方法。在 RunTask 中,我调用了 DoTheThing。 DoTheThing 可能需要一段时间才能运行,并且可能会失败(这是数据库更新)。我想确保在任何时间点,我只有一个 DoTheThing 未完成。我还想确保我没有一堆 RunTask 实例都排队等待正在运行的 RunTask 实例释放锁 做事。

public void RunTask() 
{ 
    bool canRunTask = true; 
 
    // Check if another instance of this method is currently executing.  If so, do not execute the rest of this method 
    lock (this.runTaskLock) 
    { 
        if (this.isTaskRunning) 
        { 
            canRunTask = false; 
        } 
        else 
        { 
            this.isTaskRunning = true; 
        } 
    } 
 
    // Call DoTheThing if another instance is not currently outstanding 
    if (canRunTask) 
    { 
        try 
        { 
            Task task = new Task(() => DoTheThing()); 
            task.Start(); 
        } 
        catch (Exception ex) 
        { 
            // Handle the exception 
        } 
        finally 
        { 
            lock (this.runTaskLock) 
            { 
                this.isTaskRunning = false; 
            } 
        } 
    } 
} 

由于程序的架构,我宁愿将我所有的线程同步放在这个方法中,而不是启用和禁用计时器。

请您参考如下方法:

通过略微不同地思考问题,事情变得容易多了。而不是每隔 x 触发一个计时器秒,为什么不等x调用之间的秒数?

现在您可以运行一个异步循环来完成计划的工作,并为您自己省去一大堆痛苦的同步工作。

async Task RunActionPeriodicallyAsync(Action action,  
                           TimeSpan ts,  
                           CancellationToken token = default(CancellationToken)) 
{ 
    while(!token.IsCancellationRequested) 
    { 
        action(); 
        await Task.Delay(ts, token); 
        //or alternatively (see comment below) 
        //var delayTask = Task.Delay(ts, token); 
        //action(); 
        //await delayTask; 
    } 
} 

现在,只需调用 RunActionPeriodicallyAsync一次,并且对其操作的调用永远不会重叠。

RunActionPeriodicallyAsync(() => DoSomething(), TimeSpan.FromSeconds(10)) 

您可以重载它以采取异步“操作”...实际上是一个 Func<Task> ...

async Task RunActionPeriodicallyAsync(Func<CancellationToken, Task> actionAsync,  
                           TimeSpan ts,  
                           CancellationToken token = default(CancellationToken)) 
{ 
    while(!token.IsCancellationRequested) 
    { 
        await actionAsync(token); 
        await Task.Delay(ts, token); 
        //or alternatively (see comment below) 
        //await Task.WhenAll(actionAsync(token), Task.Delay(ts, token)) 
    } 
} 

并使用它:

RunActionPeriodicallyAsync(async cancTok => await DoSomethingAsync(cancTok),  
                           TimeSpan.FromSeconds(10))