Moment For Technology

C# create thread and execute thread (dotnet core)

Posted on Aug. 8, 2022, 5:54 a.m. by Tasha Miller
Category: The back-end Tag: c#

Dotnet creates threads and executes them

Net environment.The core 2.1.5

The core code

Thread thread = new Thread(TConsoleWrite); Thread.isbackground = true; thread.isbackground = true; Thread.start (); thread.start (); //Start starts the thread of executionCopy the code

Remember to write wait: otherwise the main thread will be released when it finishes running

Complete code:

using System; using System.Threading; Namespace netcore.thread.demo {class Program {static void Main(string[] args) {Console.WriteLine(" Start thread! "); // Create a thread and name it thread thread thread = new thread (TConsoleWrite); Thread.isbackground = true; thread.isbackground = true; Thread.start (); thread.start (); System.console. WriteLine(" Press CTRL + C to stop running "); While (true) {}} static void TConsoleWrite() {// The method in the executed thread outputs system.console. WriteLine(" thread-one output "); }}}Copy the code
Search
About
mo4tech.com (Moment For Technology) is a global community with thousands techies from across the global hang out!Passionate technologists, be it gadget freaks, tech enthusiasts, coders, technopreneurs, or CIOs, you would find them all here.