Computer >> 컴퓨터 >  >> 프로그램 작성 >> C#

C#에서 스레드의 현재 컨텍스트 ID를 찾는 방법은 무엇입니까?

<시간/>

새 스레드를 만들려면.

Thread thread = Thread.CurrentThread;
thread.Name = "My new Thread”;

현재 컨텍스트 ID를 얻으려면 ContextID 속성을 사용하십시오.

Thread.CurrentContext.ContextID

전체 코드를 보자 -

예시

using System;
using System.Threading;
namespace Demo {
   class MyClass {
      static void Main(string[] args) {
         Thread thread = Thread.CurrentThread;
         thread.Name = "My new Thread";
         Console.WriteLine("Thread Name = {0}", thread.Name);
         Console.WriteLine("current Context id: {0}", Thread.CurrentContext.ContextID);
         Console.ReadKey();
      }
   }
}

출력

Thread Name = My new Thread
current Context id: 0