gpt4 book ai didi

c# - 可等待的自动重置事件

转载 作者:IT王子 更新时间:2023-10-29 04:16:29 26 4
gpt4 key购买 nike

AutoResetEvent 的异步(可等待)等价物是什么?

如果在经典的线程同步中我们会使用这样的东西:

    AutoResetEvent signal = new AutoResetEvent(false);

void Thread1Proc()
{
//do some stuff
//..
//..

signal.WaitOne(); //wait for an outer thread to signal we are good to continue

//do some more stuff
//..
//..
}

void Thread2Proc()
{
//do some stuff
//..
//..

signal.Set(); //signal the other thread it's good to go

//do some more stuff
//..
//..
}

我希望在新的异步处理方式中,会出现这样的情况:

SomeAsyncAutoResetEvent asyncSignal = new SomeAsyncAutoResetEvent();

async void Task1Proc()
{
//do some stuff
//..
//..

await asyncSignal.WaitOne(); //wait for an outer thread to signal we are good to continue

//do some more stuff
//..
//..
}

async void Task2Proc()
{
//do some stuff
//..
//..

asyncSignal.Set(); //signal the other thread it's good to go

//do some more stuff
//..
//..
}

我见过其他定制的解决方案,但我在某个时间点设法得到的解决方案仍然涉及锁定线程。我不想仅仅为了使用新的 await 语法而这样做。我正在寻找一种真正可等待的信号机制,它不会锁定任何线程。

我在任务并行库中缺少什么吗?

编辑:澄清一下:SomeAsyncAutoResetEvent 是一个完全由类名组成的类名,在我的示例中用作占位符。

最佳答案

如果您想构建自己的,Stephen Toub has the definitive blog post on the subject .

如果你想使用已经写好的,I have one in my AsyncEx library .据我所知,在撰写本文时没有其他选择。

关于c# - 可等待的自动重置事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32654509/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com