Home  >  Article  >  Backend Development  >  C# program to create a simple thread

C# program to create a simple thread

PHPz
PHPzforward
2023-09-10 16:49:121098browse

创建简单线程的 C# 程序

To create a thread, I created a function -

public void myThread() {
   for (int i = 0; i < 3; i++) {
      Console.WriteLine("My Thread");
   }
}

Call the above function to create a thread and create a new ThreadStart delegate-

Demo d = new Demo();
Thread thread = new Thread(new ThreadStart(d.myThread));

Example

Use the following code to create a simple thread.

Real-time demonstration

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
class Demo {
   public void myThread() {
      for (int i = 0; i < 3; i++) {
         Console.WriteLine("My Thread");
      }
   }
}
class NewThread {
   public static void Main() {
      Demo d = new Demo();
      Thread thread = new Thread(new ThreadStart(d.myThread));
      thread.Start();
      Console.Read();
   }
}

Output

My Thread
My Thread
My Thread

The above is the detailed content of C# program to create a simple thread. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete