如何从Winforms按钮单击事件中调用异步方法? [英] How do i call an async method from a winforms button click event?
本文介绍了如何从Winforms按钮单击事件中调用异步方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个要异步运行的I/O绑定方法.
I have an I/O bound method that I want to run asynchronously.
在帮助文档中,它提到我应该使用异步并等待没有Task.Run
In the help docs it mentions that I should use async and await without Task.Run
引用
对于绑定到I/O的代码,您等待操作返回Task或异步方法中的任务.
For I/O-bound code, you await an operation which returns a Task or Task inside of an async method.
如何通过winforms按钮单击事件来做到这一点?
How do I do this from a winforms button click event?
我尝试过
private void button_Click(object sender, EventArgs e)
{
await doLoadJob();
}
private async Task<int> doLoadJob()
{
await loadJob();
return 0;
}
推荐答案
您的 button_Click
方法必须是 async
.在 private
和 void
之间放置一个 async
.
Your button_Click
method needs to be async
. Place a async
between private
and void
.
private async void button_Click(object sender, EventArgs e)
{
await LongOperation();
}
这篇关于如何从Winforms按钮单击事件中调用异步方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文