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 combine tasks with Task.ContinueWith in C #

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

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains the "C # how to use Task.ContinueWith combination tasks", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "C # how to use Task.ContinueWith combination tasks" bar!

The code case is simple Demo

Code:

Public static void Main () {/ / create a task Task task = new Task (() = > {int sum = 0; Console.WriteLine ("use Task to perform operations."); for (int I = 0) I {Console.WriteLine ("results after task completion: {0}", t.Result.ToString ();}); task.Wait (); cwt.Wait (); Console.ReadLine (); Console.ReadKey ();}

Results:

Serial of the task

Code:

Static void Main (string [] args) {ConcurrentStack stack = new ConcurrentStack (); / / T1 first serial var T1 = Task.Factory.StartNew (() = > {/ / enter stack.Push (1); stack.Push (2);}) / / var T3 parallel execution var T2 = t1.ContinueWith (t = > {int result; / / output stack.TryPop (out result); Console.WriteLine ("Task T2 result= {0}, Thread id {1}", result, Thread.CurrentThread.ManagedThreadId);}) / / var T3 parallel execution var T3 = t1.ContinueWith (t = > {int result; / / output stack.TryPop (out result); Console.WriteLine ("Task T3 result= {0}, Thread id {1}", result, Thread.CurrentThread.ManagedThreadId);}) / wait for T2 and T3 to finish executing Task.WaitAll (T2, T3); / / T7 serial execution var T4 = Task.Factory.StartNew (() = > {Console.WriteLine ("number of previous collections: {0}, Thread id {1}", stack.Count, Thread.CurrentThread.ManagedThreadId);}) T4.Wait (); Console.ReadKey ();}

Results:

Subtask

Code:

Public static void Main () {Task parent = new Task (state = > {Console.WriteLine (state); string [] result = new string [2]; / / create and start the subtask new Task () = > {result [0] = "I am subtask 1." ;}, TaskCreationOptions.AttachedToParent) .Start (); new Task () = > {result [1] = "I am subtask 2." ;}, TaskCreationOptions.AttachedToParent). Start (); return result;}, "I am the parent, and I create multiple child tasks in the process, and I will not start running until all the child tasks are completed.") ; / / the operation parent.ContinueWith performed after task processing is completed (t = > {Array.ForEach (t.Result, r = > Console.WriteLine (r));}); / / start parent task parent.Start () / / wait for the end of the task Wait can only wait for the end of the parent thread, there is no way to wait until the ContinueWith of the parent thread ends / / parent.Wait (); Console.ReadLine ();}

Results:

Dynamic parallelism

Code:

Class Node {public Node Left {get; set;} public Node Right {get; set;} public string Text {get; set }} class Program {static Node GetNode () {Node root = new Node {Left = new Node {Left = new Node {Text = "Lmurl"}} Right = new Node {Text = "Lmurr"}, Text = "L"} Right = new Node {Left = new Node {Text = "Rmurl"} Right = new Node {Text = "Rmurr"}, Text = "R"}, Text = "Root"} Return root;} static void Main (string [] args) {Node root = GetNode (); DisplayTree (root) } static void DisplayTree (Node root) {var task = Task.Factory.StartNew (() = > DisplayNode (root), CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default) Task.Wait () } static void DisplayNode (Node current) {if (current.Left! = null) Task.Factory.StartNew (() = > DisplayNode (current.Left), CancellationToken.None, TaskCreationOptions.AttachedToParent TaskScheduler.Default) If (current.Right! = null) Task.Factory.StartNew (() = > DisplayNode (current.Right), CancellationToken.None, TaskCreationOptions.AttachedToParent, TaskScheduler.Default) Console.WriteLine ("current threshold: {0}; Thread ID processed = {1}", current.Text, Thread.CurrentThread.ManagedThreadId);}}

Results:

Thank you for your reading, the above is the content of "how to use Task.ContinueWith combination tasks in C #". After the study of this article, I believe you have a deeper understanding of the problem of how to use Task.ContinueWith combination tasks in C #, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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