English 中文(简体)
Async CTP 任务。 何时印发
原标题:Async CTP Task.WhenAll issue
  • 时间:2011-11-11 14:46:38
  •  标签:
  • c#

我对一些似乎简单但我看不到什么是错的。 我有一个简单的班级结构,即描述从复杂的计算中回来的数值,我试图找到最低值。 由于计算费昂贵,我正在创造一系列任务,以便计算工作同时进行,然后使用任务。 当所有人在比较结果(储存在StaticQuote物体)之前等待全部完成。 问题在于,在试图归还一系列法定多数时,我正经历以下错误:

不得含蓄地转换<代码> System.Threading.Tasks.Task<services. QuoteGeneratorAsync.StaticQuote[]> to Services.QuoteGeneratorAsync.StaticQuote[]

我看到了类似的例子,例如,这种派任工作做得很好,无法理解右侧没有恢复一系列法定投票结果? 我是多面阅读的法典和阿辛加特方案的新版本。 谁能提供答案? 许多感谢。

example problem:

List<Task<StaticQuote>> Calculations = new List<Task<StaticQuote>>();
foreach()
{
Calculations.Add(TaskEx.RunEx(() => Calculate(...my params....)));                                             
}
StaticQuote[] Quotes=TaskEx.WhenAll<StaticQuote>(Calculations);  --//this line won t compile
问题回答

TaskEx.WhenAll returns a Task<T[]> which indicates when all the other tasks have finished. So you want:

StaticQuote[] quotes = await TaskEx.WhenAll(Calculations);

。 因此,如果你走到别处:

Task<string> downloadTask = webClient.DownloadStringTaskAsync(url);
string result = await downloadTask;

这完全是同一件事——它只是说,<代码>。 当“所有<>/代码”版本稍加复杂,因为它收集了任务投入和产出的<><>/em>,而不是单一份。

Obviously in order to use await you have to be in an async method to start with.

如果所有这一切仍然令人困惑,你不妨读到我的blog post about async , 以及





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签