Maison  >  Article  >  développement back-end  >  Programme C# pour créer un thread simple

Programme C# pour créer un thread simple

PHPz
PHPzavant
2023-09-10 16:49:121098parcourir

创建简单线程的 C# 程序

Pour créer un fil de discussion, j'ai créé une fonction -

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

Appelez la fonction ci-dessus pour créer un fil de discussion et créer un nouveau délégué ThreadStart -

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

Exemple

Utilisez le code suivant pour créer un fil de discussion simple.

Démo en temps réel

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

Sortie

My Thread
My Thread
My Thread

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Cet article est reproduit dans:. en cas de violation, veuillez contacter admin@php.cn Supprimer