首页  >  文章  >  后端开发  >  创建简单线程的 C# 程序

创建简单线程的 C# 程序

PHPz
PHPz转载
2023-09-10 16:49:121098浏览

创建简单线程的 C# 程序

为了创建线程,我创建了一个函数 -

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

调用上面的函数来创建一个线程,并创建一个新的 ThreadStart 委托 -

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

示例

使用以下代码创建一个简单的线程。

实时演示

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

输出

My Thread
My Thread
My Thread

以上是创建简单线程的 C# 程序的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:tutorialspoint.com。如有侵权,请联系admin@php.cn删除