如果您只有一次通知,则可以使用TaskCompletionSource
:
public class Foo
{
private TaskCompletionSource<object> _signal = new TaskCompletionSource<object>();
public void UnblockDoSomething()
{
DoWork();
_signal.SetResult(null);
_signal = new TaskCompletionSource<object>();
}
public async Task DoSomethingAsync()
{
var continueSignal = _signal.Task;
DoSomeWork();
await continueSignal;
DoMoreWork();
}
}
另一种选择是使用信号量 ( SemaphoreSlim
),如果之前已发出信号,它将“记住”:
public class Foo
{
private readonly SemaphoreSlim _mutex = new SemaphoreSlim(0);
public void UnblockDoSomething()
{
DoWork();
_mutex.Release();
}
public async Task DoSomethingAsync()
{
DoSomeWork();
await _mutex.WaitAsync();
DoMoreWork();
}
}
如果你真的需要一个条件变量,你可以AsyncConditionVariable
从我的AsyncEx 库中使用:
public class Foo
{
private readonly AsyncLock _mutex = new AsyncLock();
private readonly AsyncConditionVariable _cv = new AsyncConditionVariable(_mutex);
public void UnblockDoSomething()
{
using (await _mutex.LockAsync())
{
DoWork();
_cv.Notify();
}
}
public async Task DoSomethingAsync()
{
using (await _mutex.LockAsync())
{
DoSomeWork();
await _cv.WaitAsync();
DoMoreWork();
}
}
}