2010-02-16 8 views

답변

2

스레드가 시작되지 않는 한 리플렉션을 사용할 수 있지만 그런 유용성은 매우 의심 스럽습니다. 왜 대신 다른 스레드 인스턴스를 만들 수 없습니다 :

당신은 디버깅 API와 이상한 일을하거나 CLR 호스팅 및 내부 함께 연주하지 않는
var thread = new Thread(Method1); 
thread 
    .GetType() 
    .GetMethod("SetStartHelper", BindingFlags.Instance | BindingFlags.NonPublic) 
    .Invoke(thread, new object[] { new ThreadStart(Method2), 0 }); 
thread.Start(); 
+0

참고,이하지 않습니다 어떤 효과가 있습니다. –

+0

예, 그렇기 때문에 '스레드가 시작되지 않은 한 ...'으로 대답을 시작했습니다. –

1

는 스레드가 새로 '재 할당'할 수있는 방법은 없습니다 메서드는 스레드 스택을 조작하는 것과 관련이 있습니다. 당신이 할 수있는 일

이 시간에서 변경 될 수 있습니다 '페이로드'일을 보낼 스레드에서 명령 패턴을 사용하는 것입니다 :이 호출하면 스레드가 시작된 후 것을

 Action worker=null; 
     bool running = true; 
     var t = new Thread(() => 
          { 
           while(running) 
           { 
            Action theWorker = worker; 
            if(theWorker!=null) 
            { 
             theWorker();  
            } 
            Thread.Sleep(10); 
           } 
          }); 
     worker = new Action(() => Console.WriteLine("hi mum")); 

     t.Start(); 
     Thread.Sleep(100); 
     worker=new Action(()=>Console.WriteLine("I can see my house from here")); 
     Thread.Sleep(100); 
     running = false;