Maison  >  Article  >  développement back-end  >  Programme C# qui transmet les paramètres aux threads

Programme C# qui transmet les paramètres aux threads

WBOY
WBOYavant
2023-08-27 10:25:09818parcourir

将参数传递给线程的 C# 程序

Pour utiliser les threads, ajoutez l'espace de noms suivant dans votre code -

using System.Threading;

Tout d'abord, vous devez créer un nouveau thread en C# -

Thread thread = new Thread(threadDemo);

Ci-dessus, threadDemo est notre fonction de thread.

Maintenant, transmettez les paramètres au thread -

thread.Start(str);

Les paramètres définis ci-dessus sont -

String str = "Hello World!";

Exemple

Voyons le code complet pour passer les paramètres au thread en C#.

Démo en temps réel p>

using System;
using System.Threading;
namespace Sample {
   class Demo {
      static void Main(string[] args) {
         String str = "Hello World!";
         // new thread
         Thread thread = new Thread(threadDemo);
         // passing parameter
         thread.Start(str);
      }
      static void threadDemo(object str) {
         Console.WriteLine("Value passed to the thread: "+str);
      }
   }
}

Sortie

Value passed to the thread: Hello World!

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