如何使用任务时ContinueWith从previous任务结果的另一个功能?

编程入门 行业动态 更新时间:2024-10-25 02:31:02
本文介绍了如何使用任务时ContinueWith从previous任务结果的另一个功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有应该得到一些少量的数据为我的WCF连接器,通常它需要长达20秒,以得到这个数据的每个项(这是细)。我想用任务来获得我的数据,然后用值从这个任务中添加的WinForm控件。

I have WCF connector that should get some small amount of data for me, usually it takes up to 20 seconds to get this data for each item ( which is fine ). I want to use Task to get data for me and then add WinForm controls with value from this Tasks.

我创建的对象的列表,其中将包括此数据。

I've created list of objects which will consist this data.

用于第一项任务作为其中一个更新列表,我想任务就是马上后,第一项任务是为了创建控件。

Used first Task as the one which updates the list and i want Task that is right away after first Task is done to create controls.

这是在code到目前为止:

This is the code so far:

List<IpVersionCounter> ipVersionCounters = new List<IpVersionCounter>(); Task task = Task.Factory.StartNew(() => { foreach (var sitein settings.Sites) { string ip = site.ip; string version = "undefined"; using (WcfConnector wcfConnector = WcfConnector.CreateConnectorWithoutException((ip))) { if (wcfConnector != null) { version= string.Format("{0} {1} {2}", wcfConnector.VersionController.GetBranchName(), wcfConnector.VersionController.GetBuildNumber(), wcfConnector.VersionController.GetCurrentVersion()); } } counter++; ipVersionCounters.Add(new IpVersionCounter { Ip = ip, Version = Version, Counter = counter }); } return ipVersionCounters; }).ContinueWith(); AddProgressBar(ipVersionCounter);

我不知道如果我要正确的方式,以及如何使用ContinueWith从第一种方法传递值第二。

I don't know if i'm going right way and how to use ContinueWith to pass value from first method to second.

推荐答案

在低于T的例子引用previous任务,使用结果属性从它那里得到的返回值。

In the example below t references the previous task, use the Result property to get the return value from it.

Task task = Task.Factory.StartNew(() => { // Background work }).ContinueWith((t) => { var ipVersionCounters = t.Result; });

更新

如果您希望continuewith对UI线程使用EXECUTE(如果你开始在UI线程)

If you want the continuewith to execute on the UI thread use (If you are starting on the UI thread) ...

Task.Factory.StartNew(() => { // Background work }).ContinueWith((t) => { // Update UI thread }, TaskScheduler.FromCurrentSynchronizationContext());

(其从this回答获取更多信息)

(which was taken from this answer for more info)

更多推荐

如何使用任务时ContinueWith从previous任务结果的另一个功能?

本文发布于:2023-11-24 05:33:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1624116.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   功能   ContinueWith   previous

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!