gpt4 book ai didi

c# - 同步等待异步方法在同一线程上完成

转载 作者:行者123 更新时间:2023-11-30 21:59:13 24 4
gpt4 key购买 nike

有没有办法同步等待在同一线程上运行的异步方法?

想要的效果是

  • 让 Worker() 在 UI 线程上异步运行
  • 同时等待它在 Close() 方法返回之前完成

下面的例子进入了死锁状态,如果我让 Form1_FormClosing() 异步,我不满足第二个条件。

public partial class Form1 : Form
{
TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
CancellationTokenSource cts = new CancellationTokenSource();
public Form1()
{
InitializeComponent();
Show();
Worker(cts.Token); // async worker started on UI thread
}

async void Worker(CancellationToken ct)
{
while (!ct.IsCancellationRequested)
await TaskEx.Delay(1000);
tcs.SetResult(true); // signal completition
}

private void button1_Click(object sender, EventArgs e)
{
Close();
MessageBox.Show("This is supposed to be second");
}

private async void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
cts.Cancel(); // request cancel
tcs.Task.Wait(); // deadlock
await tcs.Task; // button1_Click() gets control back instead of Worker()
MessageBox.Show("This is supposed to be first");
}
}

最佳答案

Is there a way to wait synchronously for an async method that runs on the same thread?

您不需要同步等待。通过制作 Worker async Task 而不是 async void 您可以获得所需的行为并删除无用的 TaskCompletionSource:

private Task workerTask;
public Form()
{
workerTask = Worker(cts.Token);
}

private async Task Worker(CancellationToken ct)
{
while (!ct.IsCancellationRequested)
await TaskEx.Delay(1000);
}

private async void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
cts.Cancel(); // request cancel
await workerTask; // Wait for worker to finish before closing
}

我缺少 Close() 的实现,但我怀疑您可以不用它并通过表单关闭事件来取消工作人员。

关于c# - 同步等待异步方法在同一线程上完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29453337/

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