In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Most people do not understand the knowledge points of this article "how to use the C# continuous task Task.ContinueWith method", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "C# continuous task Task.ContinueWith method how to use" article.
I. brief introduction
With tasks, you can specify that after the task is complete, you should start running another specific task. A ContinueWith is a Task that decides what to do next according to its own situation. In other words, after running task, the XX statement in task.continuewith (XX) is executed, but whether and how to execute it depends on the operation of 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 sequential processing methods of a task have a parameter of type Task.
II. Case 1:
Code:
Static int TaskMethod (string name, int seconds) {Console.WriteLine ("Frist TaskMethod: Task {0} is running on a thread id: {1}. Is thread pool thread: {2} ", name, Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.IsThreadPoolThread); Thread.Sleep (TimeSpan.FromSeconds (seconds)); return 60 * seconds;} static void Main (string [] args) {var FirstTask = new Task (() = > TaskMethod (" Frist Task ", 3)) FirstTask.ContinueWith (t = > Console.WriteLine ("Frist Task Result: {0}). Thread id: {1}, Is in Thred Pool: {2} ", t.Result, Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.IsThreadPoolThread), TaskContinuationOptions.OnlyOnRanToCompletion); / / process pool FirstTask.Start (); Console.ReadKey ();}
Results:
The order of Start () and ContinueWith () does not matter, ContinueWith () waits until the running state of firstTask reaches IsCompleted, because of the OnlyOnRanToCompletion in TaskContinuationOptions. It must be pointed out that the parameters in ContinueWith () need to take Task as a parameter, that is, firstTask is passed as a parameter, and ContinueWith () runs in a thread in the thread pool. I think it's important to treat the statements in ContinueWith () as a new block of statements, which are independent of the main thread. In any case, they are judged, if the status is not satisfied, then they do not execute; when more than one state is specified, a reasonable corresponding state is used.
Case 2:
Code:
Class Program {static int TaskMethod (string name, int seconds) {Console.WriteLine ("TaskMethod: Task {0} is running on a thread id: {1}. Is thread pool thread: {2} ", name, Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.IsThreadPoolThread); Thread.Sleep (TimeSpan.FromSeconds (seconds)); return 60 * seconds;} static void Main (string [] args) {Console.WriteLine (" Main Thread id {0}, Is in Thred Pool: {1} ", Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.IsThreadPoolThread) Var firstTask = new Task (() = > TaskMethod ("frist task", 3)); var secondTask = new Task (() = > TaskMethod ("second task", 2)); firstTask.ContinueWith (t = > Console.WriteLine ("Result:Frist Thread Result: {0}). Thread id: {1}, Is in Thred Pool: {2} ", t.Result, Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.IsThreadPoolThread), TaskContinuationOptions.OnlyOnRanToCompletion); firstTask.Start (); secondTask.Start (); Thread.Sleep (TimeSpan.FromSeconds (4)); / / give enough time for firstTask, secondTask and their subsequent operations to complete. Task continuation = secondTask.ContinueWith (t = > Console.WriteLine ("Result:Second Thread Result: {0}. Thread id: {1}, Is in Thred Pool: {2}", t.Result, Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.IsThreadPoolThread), TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.ExecuteSynchronously); Console.ReadLine (); Console.ReadKey ();}}
Results:
Here, the main thread hibernates for a full 4 seconds, enough for both firstTask and secondTask tasks to run, and then, due to the secondTask's subsequent acceptance of ExecuteSynchronously in addition to OnlyOnRanToCompletion. Therefore, in the subsequent run, the ExecuteSynchronously is recognized because the main thread has not been finished, so the subsequent secondTask runs on the main thread.
Case 3:
Code:
Class Program {static int TaskMethod (string name, int seconds) {Console.WriteLine ("TaskMethod: Task {0} is running on a thread id: {1}. Is thread pool thread: {2} ", name, Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.IsThreadPoolThread); Thread.Sleep (TimeSpan.FromSeconds (seconds)); return 60 * seconds;} static void Main (string [] args) {Console.WriteLine (" Main Thread id {0}, Is in Thred Pool: {1} ", Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.IsThreadPoolThread) Var firstTask = new Task (() = > TaskMethod ("frist task", 3)); var secondTask = new Task (() = > TaskMethod ("second task", 2)); firstTask.ContinueWith (t = > Console.WriteLine ("Result:Frist Thread Result: {0}). Thread id: {1}, Is in Thred Pool: {2} ", t.Result, Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.IsThreadPoolThread), TaskContinuationOptions.OnlyOnRanToCompletion); firstTask.Start (); secondTask.Start (); / / Thread.Sleep (TimeSpan.FromSeconds (4)); / / enough time for firstTask, secondTask and subsequent operations to be completed. Task continuation = secondTask.ContinueWith (t = > Console.WriteLine ("Result:Second Thread Result: {0}. Thread id: {1}, Is in Thred Pool: {2}", t.Result, Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.IsThreadPoolThread), TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.ExecuteSynchronously); Console.ReadLine (); Console.ReadKey ();}}
Results:
However, if you comment out the 4-second hibernation, because the main thread ends early, secondTask can only accept OnlyOnRanToCompletion, so it is still running in the thread pool.
The above is about the content of this article on "how to use the Task.ContinueWith method of C# continuous tasks". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.