[英]Allow async method to be called only one instance at a time
I have a method which cannot be executed in multiple threads simultaneously (it writes to a file).我有一个不能在多个线程中同时执行的方法(它写入一个文件)。 I cannot use
lock
, because the method is async
.我不能使用
lock
,因为该方法是async
。 How to avoid calling the method in another thread?如何避免在另一个线程中调用该方法? Instead of calling it a second time, the program should wait for the previous call to complete (also asynchronously).
程序应该等待前一次调用完成(也是异步的),而不是第二次调用它。
For example, create a new C# console application with the following code:例如,使用以下代码创建一个新的 C# 控制台应用程序:
using System;
using System.IO;
using System.Threading.Tasks;
namespace ConsoleApplication1 {
internal class Program {
private static void Main () {
CallSlowStuff();
CallSlowStuff();
Console.ReadKey();
}
private static async void CallSlowStuff () {
try {
await DoSlowStuff();
Console.WriteLine("Done!");
}
catch (Exception e) {
Console.WriteLine(e.Message);
}
}
private static async Task DoSlowStuff () {
using (File.Open("_", FileMode.Create, FileAccess.Write, FileShare.None)) {
for (int i = 0; i < 10; i++) {
await Task.Factory.StartNew(Console.WriteLine, i);
await Task.Delay(100);
}
}
}
}
}
In this example, the second call to CallSlowStuff
throws an exception, because it cannot access a file which is already opened.在此示例中,对
CallSlowStuff
的第二次调用引发异常,因为它无法访问已打开的文件。 Adding lock
is not an option, because lock
and async
don't mix.添加
lock
不是一种选择,因为lock
和async
不混合。 ( Main
method should be considered unchangable. In a real application, CallSlowStuff
is an interface method which can be called anywhere.) (
Main
方法应该被认为是不可CallSlowStuff
。在实际应用程序中, CallSlowStuff
是一个可以在任何地方调用的接口方法。)
Question: How to make subsequent calls to CallSlowStuff
wait for the currently running call to complete, without blocking the main thread?问题:如何让
CallSlowStuff
后续调用等待当前运行的调用完成,而不阻塞主线程? Can it be done using just async/await and tasks (and Rx maybe)?是否可以仅使用 async/await 和任务(可能还有 Rx)来完成?
You need some sort of async lock.您需要某种异步锁。 Stephen Toub has a whole series of articles about building
async
synchronization primitives (including AsyncLock
). Stephen Toub 有一系列关于构建
async
同步原语(包括AsyncLock
)的文章。 A version of AsyncLock
is also contained in Stephen Cleary's AsyncEx library . AsyncLock
一个版本也包含在 Stephen Cleary 的AsyncEx 库中。
But probably a simpler solution would be to use the built-in SemaphoreSlim
, which does support asynchronous waiting:但可能更简单的解决方案是使用内置的
SemaphoreSlim
,它支持异步等待:
private static SemaphoreSlim SlowStuffSemaphore = new SemaphoreSlim(1, 1);
private static async void CallSlowStuff () {
await SlowStuffSemaphore.WaitAsync();
try {
await DoSlowStuff();
Console.WriteLine("Done!");
}
catch (Exception e) {
Console.WriteLine(e.Message);
}
finally {
SlowStuffSemaphore.Release();
}
}
I would consider altering the method body of CallSlowStuff
to post messages to a TPL DataFlow ActionBlock, and configuring it to a single degree of parallelism:我会考虑更改
CallSlowStuff
的方法主体以将消息发布到TPL DataFlow ActionBlock,并将其配置为单一并行度:
So keep a single ActionBlock somewhere:所以在某处保留一个 ActionBlock:
ActionBlock actionBlock =
new ActionBlock<object>(
(Func<object,Task>)CallActualSlowStuff,
new ExecutionDataflowBlockOptions(){MaxDegreeOfParallelism=1});
Now:现在:
public void CallSlowStuff()
{
actionBlock.Post(null);
}
private async Task CallActualSlowStuff(object _)
{
using (File.Open("_", FileMode.Create, FileAccess.Write, FileShare.None)) {
for (int i = 0; i < 10; i++) {
await Task.Factory.StartNew(Console.WriteLine, i);
await Task.Delay(100);
}
}
}
You can use a Semaphore in this instance, see http://msdn.microsoft.com/en-us/library/system.threading.semaphore.aspx您可以在这种情况下使用信号量,请参阅http://msdn.microsoft.com/en-us/library/system.threading.semaphore.aspx
Something like:就像是:
private Semaphore sem = new Semaphore(1, 1);
private static async Task DoSlowStuff () {
sem.WaitOne();
// your stuff here
sem.Release();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.