>  기사  >  백엔드 개발  >  매개변수를 스레드에 전달하는 C# 프로그램

매개변수를 스레드에 전달하는 C# 프로그램

WBOY
WBOY앞으로
2023-08-27 10:25:09819검색

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

스레드를 사용하려면 코드에 다음 네임스페이스를 추가하세요. -

using System.Threading;

먼저 C#에서 새 스레드를 만들어야 합니다. -

Thread thread = new Thread(threadDemo);

위에서 threadDemo는 스레드 함수입니다.

이제 매개변수를 스레드에 전달 -

thread.Start(str);

위에 설정된 매개변수는 -

String str = "Hello World!";

Example

C#에서 스레드에 매개변수를 전달하는 전체 코드를 살펴보겠습니다.

실시간 데모 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);
      }
   }
}

Output

Value passed to the thread: Hello World!

위 내용은 매개변수를 스레드에 전달하는 C# 프로그램의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 tutorialspoint.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제