简体   繁体   English

投下任务 <T> 到了T

[英]Cast a Task<T> to a T

How do I cast an Task<IEnumerable<IMapLocationItem>> to an IEnumerable<IMapLocationItem> ? 如何将Task<IEnumerable<IMapLocationItem>>转换为IEnumerable<IMapLocationItem>

Here is my code that gets a Task<IEnumerable<IMapLocationItem>> : 这是我的代码获取Task<IEnumerable<IMapLocationItem>>

public async Task<IEnumerable<IMapLocationItem>> GetAllMapLocationItemsFromUserName(string userName)
{
    IEnumerable<IMapLocationItem> mapLocationImageGalleries = await db.mapLocationImageGalleries.Where(m => m.userName.Equals(userName)).ToListAsync();
    IEnumerable<IMapLocationItem> mapLocationVideoGalleries = await db.mapLocationVideoGalleries.Where(m => m.userName.Equals(userName)).ToListAsync();

    IEnumerable<IMapLocationItem> mapLocationItemsCombined = mapLocationImageGalleries.Concat(mapLocationVideoGalleries);
    return mapLocationItemsCombined;
}

I can use the keyword .Result , but I have read somewhere that this prevents the async task from being asynchronous, and this method takes a very long time to finish when using the Result keyword. 我可以使用关键字.Result ,但我在某处读过这会阻止async任务异步,并且在使用Result关键字时,此方法需要很长时间才能完成。

How is the best way to cast a Task<IEnumerable<IMapLocationItem>> to an IEnumerable<IMapLocationItem> ? 如何将Task<IEnumerable<IMapLocationItem>>转换为IEnumerable<IMapLocationItem>

Thanks in advance 提前致谢

You can't cast a Task<T> to T , they represent different things. 你不能将Task<T>T ,它们代表不同的东西。 A Task<T> represents a task that in the future will eventually return a T . Task<T>表示将来最终返回T If you want to get that result of a task you have several options (assuming t = Task<T> ) 如果你想得到一个任务的结果你有几个选项(假设t = Task<T>

  • t.Result blocks the execution until the result is available. t.Result阻止执行直到结果可用。 This has the unfortunate side effect that it might deadlock in some cases, most notably when combined with async methods in UI threads. 这有一个令人遗憾的副作用,它可能在某些情况下会死锁,最明显的是当与UI线程中的async方法结合使用时。
  • await t schedules a continuation and runs it once the result is available. await t计划一个延续并在结果可用后运行它。 Only available with C# 5 and .NET 4.5 or .NET 4 with the Microsoft.Bcl.Async library. 仅适用于带有Microsoft.Bcl.Async库的C#5和.NET 4.5或.NET 4。
  • t.ContinueWith schedules a continuation and runs it once the result is available. t.ContinueWith计划延续并在结果可用后运行它。

In general I prefer the use of await or ContinueWith , but sometimes using Result is the easiest, especially if you have access to the source of both the calling and called method and thus can ensure that no deadlock occurs. 通常我更喜欢使用awaitContinueWith ,但有时使用Result是最简单的,特别是如果你有权访问调用和被调用方法的源,从而可以确保不会发生死锁。

From the MSDN website http://msdn.microsoft.com/en-us/library/hh191443.aspx 来自MSDN网站http://msdn.microsoft.com/en-us/library/hh191443.aspx

 // Signature specifies Task<TResult>
 async Task<int> TaskOfTResult_MethodAsync()
 {
     int hours;
     // . . .
     // Return statement specifies an integer result.
     return hours;
 }

 // Calls to TaskOfTResult_MethodAsync
 Task<int> returnedTaskTResult = TaskOfTResult_MethodAsync();
 int intResult = await returnedTaskTResult;
 // or, in a single statement
 int intResult = await TaskOfTResult_MethodAsync();

You cannot cast a task to the result. 您无法将任务强制转换为结果。 That defeats the purpose of async. 这违背了异步的目的。 What the code above does is calls the function asynchronously, then later in the code requests the result by calling "await". 上面的代码是异步调用函数,然后在代码中通过调用“await”来请求结果。 This will wait for the asnyc function to finish (will block the code until that is done) until the result is ready. 这将等待asnyc函数完成(将阻止代码直到完成),直到结果准备好。

The issue is that you cannot predict when something will be done when it is running asynchronously, so instead we tell the Task when we are finally ready to wait for the result. 问题在于,当异步运行时,你无法预测何时会完成某些事情,所以我们告诉Task什么时候我们终于准备好等待结果了。 It could be done right away or in 5 minutes. 它可以立即完成或在5分钟内完成。 Await will wait for as long as it is necessary for the function to finish. 只要有必要完成该功能,Await将等待。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM