Home > Backend Development > C#.Net Tutorial > C# program to create a simple thread

C# program to create a simple thread

PHPz
Release: 2023-09-10 16:49:12
forward
1162 people have browsed it

创建简单线程的 C# 程序

To create a thread, I created a function -

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

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));
Copy after login

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();
   }
}
Copy after login

Output

My Thread
My Thread
My Thread
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template