分别定义和链接任务

发布于 2024-12-29 08:09:01 字数 506 浏览 3 评论 0 原文

我希望我能以某种方式定义然后以一种很好的方式进行链接以获得一些类似于此的代码:

Task ShowLoadNotificationTask = new Task( () => ShowLoadNotification(frm) );
Task LoadAllWorkOrdersTask = new Task(() => LoadAllWorkOrders());
Task CloseLoadNotificationTask = new Task(() => CloseLoadNotificationTask(frm));

ShowLoadNotificationTask.ContinueWith( LoadAllWorkOrdersTask );
LoadAllWorkOrdersTask..ContinueWith(CloseLoadNotificationTask);

ShowLoadNotificationTask.Start();

当然上面的代码是不正确的,但是如何获得与上面类似的工作?

I was hoping that I could somehow define and then chainem up in a nice way to get some code similar to this:

Task ShowLoadNotificationTask = new Task( () => ShowLoadNotification(frm) );
Task LoadAllWorkOrdersTask = new Task(() => LoadAllWorkOrders());
Task CloseLoadNotificationTask = new Task(() => CloseLoadNotificationTask(frm));

ShowLoadNotificationTask.ContinueWith( LoadAllWorkOrdersTask );
LoadAllWorkOrdersTask..ContinueWith(CloseLoadNotificationTask);

ShowLoadNotificationTask.Start();

Of course the above code is not correct, but how to get something working similar to above?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光匆匆的小流年 2025-01-05 08:09:01

听起来您想独立构造 Task 实例,然后将它们组合成一个 Task。如果是这样,那么您正在寻找的是 System.Core 中定义的 Unwrap 扩展方法

Task parent = ShowLoadNotificationTask
  .ContinueWith(_ => LoadAllWorkOrdersTask)
  .Unwrap()
  .ContinueWith(_ => CloseLoadNotificationTask)
  .Unwrap();

Unwrap 方法采用 Task 并返回返回一个 TaskTask> 也是如此。这允许您返回一个 Task 作为某个任务的延续现有的 Task 并获取您返回的原始 Task 类型。

It sounds like you want to independently construct Task instances and then later combine them together into a single Task. If so then what you are looking for is the Unwrap extension method defined in System.Core

Task parent = ShowLoadNotificationTask
  .ContinueWith(_ => LoadAllWorkOrdersTask)
  .Unwrap()
  .ContinueWith(_ => CloseLoadNotificationTask)
  .Unwrap();

The Unwrap method takes a Task<Task> and returns back a Task (the same goes for Task<Task<T>>. This allows you to return a Task as the continuation of an existing Task and get back the original Task type you returned.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文