我可以编写通用扩展方法来运行任务吗? [英] Can I write generic extension method to run task?

查看:98
本文介绍了我可以编写通用扩展方法来运行任务吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打电话给这个方法:
Windows.Foundation.IAsyncOperation< AppServiceResponse> AppServiceConnection.SendMessageAsync(ValueSet消息)



例如。 await connection.SendMessageAsync(initialStatus);



它显示一个错误:'IAsyncOperation'不包含GetAwaiter和没有扩展方法GetAwaiter接受类型'IAsyncOperation'的第一个参数可以找到(你是否缺少'系统'使用指令?)

然后我将它改为:

  await Task.Run((=)=> connection.SendMessageAsync(initialStatus)); 

它已经被编译。



我的第一个问题是为什么我不能等待SendMessageAsync?这意味着什么没有定义GetAwaiter?

第二个问题是我还需要运行其他方法
例如 Windows.Foundation.IAsyncOperation< AppServiceConnectionStatus> AppServiceConnection.OpenAsync()



我写了一个扩展方法:

  public static async void AsTask(this IAsyncOperation< AppServiceResponse> operation)
{
await Task.Run((=)=> operation);





$ b因此我可以调用 connection.SendMessageAsync(initialStatus) .AsTask(); 但是我可以使扩展方法通用,所以我也可以调用类似于 connection.OpenAsync()。AsTask();

解决方案


我的第一个问题是为什么我不能等待SendMessageAsync?这意味着没有定义GetAwaiter?


这意味着你缺少对的引用, System.Runtime.WindowsRuntime.dll


然后我将它改为[use Task.Run ]编译完成。


是的,但它不能正常工作。正如mike所说的,这里的 Task.Run 的返回类型是 Task< IAsyncOperation< T>> , (不等待)实际的 IAsyncOperation< T> 。所以你只是在后台线程上启动这个操作,然后等待那个操作启动 - 你并没有等待它完成 >。



所以,你不应该写一个扩展方法。你不应该在这里使用 Task.Run 。只需添加缺少的参考,你应该没问题。


I am calling this method: Windows.Foundation.IAsyncOperation<AppServiceResponse> AppServiceConnection.SendMessageAsync(ValueSet message)

e.g. await connection.SendMessageAsync(initialStatus);

It shows an error: 'IAsyncOperation' does not contain a definition for "GetAwaiter" and no extension method "GetAwaiter" accepting a first argument of type 'IAsyncOperation'could be found (are you missing a using directive for 'System'?)

Then I change it to:

await Task.Run(() => connection.SendMessageAsync(initialStatus));

It got compiled.

My first question is why I cannot apply await to SendMessageAsync? What exactly does it mean "no definition for GetAwaiter"?

My second question is I also need to run other methods e.g. Windows.Foundation.IAsyncOperation<AppServiceConnectionStatus> AppServiceConnection.OpenAsync()

I write an extension method:

public static async void AsTask(this IAsyncOperation<AppServiceResponse> operation)
{
    await Task.Run(() => operation);
}

So I can call connection.SendMessageAsync(initialStatus).AsTask(); But can I make the extension method generic so I can also call something like connection.OpenAsync().AsTask();

解决方案

My first question is why I cannot apply await to SendMessageAsync? What exactly does it mean "no definition for GetAwaiter"?

It means you're missing a reference to System.Runtime.WindowsRuntime.dll.

Then I change it to [use Task.Run] It got compiled.

Yes, but it won't work right. As mike commented, the return type of Task.Run here is Task<IAsyncOperation<T>>, which when awaited will return (not await) the actual IAsyncOperation<T>. So you're just starting the operation on a background thread and then awaiting for that operation to start - you're not awaiting for it to complete.

So, you shouldn't write an extension method. You shouldn't be using Task.Run at all here. Just add the missing reference, and you should be fine.

这篇关于我可以编写通用扩展方法来运行任务吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆