用于创建简单线程的 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

更新时间: 19-Jun-2020

488 次浏览

启动您的 职业

通过完成课程来获得认证

开始
广告
© . All rights reserved.