Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Task.ContinueWith continuous tasks in C #

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article is about how to use Task.ContinueWith continuous tasks in C #. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

With tasks, you can specify that after the task is complete, you should start running another specific task. For example, a new task that uses the results of the previous task should perform some cleanup if the previous task fails. Task handlers do not take a parameter or an object parameter, while the continuous processing methods of a task have a parameter of type Task. Here, you can access the relevant information about the starting task:

Such as the following sample code:

Class Program {static void DoOnFirst () {Console.WriteLine ($"doing some task {Task.CurrentId}"); Thread.Sleep (3000);} static void DoOnSecond (Task t) {Console.WriteLine ($"task {t.Id} finished"); Console.WriteLine ($"this task id {Task.CurrentId}") Console.WriteLine ("doing some cleanup"); Thread.Sleep (3000);} static void Main (string [] args) {Task T1 = new Task (DoOnFirst); t1.Start (); Task T2 = t1.ContinueWith (DoOnSecond); Task T3 = t1.ContinueWith (DoOnSecond) Task T4 = t2.ContinueWith (DoOnSecond); Console.ReadKey ();}}

Consecutive tasks are defined by calling the ContinueWith () method on the task. You can also use the TaskFactory class to define it. The t1.ContinueWith (DoOnSecond) method indicates that a new task that calls the DoOnSecond () method should be started immediately at the end of task T1. At the end of a task, multiple tasks can be started, and consecutive tasks can have another consecutive task. As shown in the following example:

Task T1 = new Task (DoOnFirst); t1.Start (); Task T2 = t1.ContinueWith (DoOnSecond); Task T3 = t1.ContinueWith (DoOnSecond); Task T4 = t2.ContinueWith (DoOnSecond)

Using the values in the TaskCreationOptions enumeration, you can specify that consecutive tasks start only when the initial task ends successfully (or fails). Some possible values are OnlyOnFaulted, NotOnFaulted, OnlyOnCanceled, NotOnCanceled, and OnlyOnRanToCompletion.

Task T5 = t1.ContinueWith (DoOnError, TaskContinuationOptions.OnlyOnFaulted) C # Task.ContinueWith returns the object

Logic that uses Task.ContinueWith to continue execution when the Task is complete

When you need to return an object in ContinueWith, how to use the

The following code can indicate that after setting up the cache, reread the cache and return

Var item = await SetCacheItemAsync ("key", cacheItems) .ContinueWith (async _ = > await cacheItem.GetAsync (key)); thank you for reading! This is the end of this article on "how to use Task.ContinueWith continuous tasks in C#". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report