C # how to start a thread through a task
This article will explain in detail how C # starts threads through tasks. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
Start a thread through a task
1 > Task starts threads
Using System;using System.Threading;using System.Threading.Tasks; public static void Main (string [] args) {Task t=new Task (DownLoadFile_My); t.Start (); Console.WriteLine ("main ()"); Console.ReadKey ();} static void DownLoadFile_My () {Console.WriteLine ("start downloading... thread ID:" + Thread.CurrentThread.ManagedThreadId); Thread.Sleep (2000); Console.WriteLine ("download complete!") ;}
Running result:
2 > TaskFactory starts threads
Public static void Main (string [] args) {TaskFactory tf=new TaskFactory (); tf.StartNew (DownLoadFile_My); Console.WriteLine ("main ()"); Console.ReadKey ();} static void DownLoadFile_My () {Console.WriteLine ("start downloading... thread ID:" + Thread.CurrentThread.ManagedThreadId); Thread.Sleep (2000); Console.WriteLine ("download complete!") ;}
Running result:
This is the end of the article on "how to start threads through tasks". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.