问题描述
我彻底整糊涂等待在C#/异步模式。
I'm thoroughly confused by the whole await / async pattern in C#.
我有一个表格应用程序,我想调用了一个20秒的方法做一吨的处理。因此,我想的await
它。我认为正确的方法是将其标记为异步任务
但是这样做会产生警告,因为我不使用的await
内的任何地方。
I have a forms app, and I want to call a method that takes 20 seconds to do a ton of processing. Therefore I want to await
it. I thought the correct way was to mark it as async Task
but doing this produces a warning because I don't use await
anywhere within it.
一个谷歌透露一些关于返回 TaskCompletionSource< T>
,但我没有返回类型,因为它是无效的。
A google revealed something about returning a TaskCompletionSource<T>
but I don't have a return type, since it's void.
我怎么能说用这种方法等待
?
How can I call this method using await
?
推荐答案
打电话给你的方法如下:
Call your method as following:
await Task.Run(() => YourMethod());
当你使用它为您创建一个awaitable任务Task.Run方法。
When you use the Task.Run method it creates an awaitable task for you.
这篇关于等待一个非异步方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!