Get the App
SLTechnology News&Howtos  ›  Development  › 

C # how to start a thread through a task

Shulou Source: shulou.com Published: 2022-06-01 02:09:42 09月21日 Update

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.

Tags: Threads tasks articles clips more results run good practical content articles knowledge references help related Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Apple Shulou Tech Info vpn macOS