Foo.MyMethod
이 이벤트를 트리거하면 Foo.MyMethod
이 풀의 스레드에서 실행되므로 이벤트 콜백도 풀의 스레드에서 실행됩니다. 이 확인하기 쉬운 : 내 콘솔
public class MyClass
{
public EventHandler OnSomeEvent;
public void MyMethod(object state)
{
OnSomeEvent(null, null);
}
}
class Program
{
static void Main()
{
Console.WriteLine(
"main thread id: {0}",
Thread.CurrentThread.GetHashCode()
);
MyClass Foo = new MyClass();
Foo.OnSomeEvent += new EventHandler(Foo_SomeEvent);
ThreadPool.QueueUserWorkItem(Foo.MyMethod, null);
Console.ReadKey();
}
static void Foo_SomeEvent(object sender, EventArgs e)
{
Console.WriteLine(
"Foo_SomeEvent thread id: {0}",
Thread.CurrentThread.GetHashCode()
);
}
}
인쇄 : 증명 그것에
main thread id: 1
Foo_SomeEvent thread id: 3
좋은 예. 감사 –