2011-09-10 3 views
5

System.Windows.Forms.Application.Run() 다른 스레드에서 호출하여 발급 된 메시지 루프를 완전히 종료하는 방법이 있습니까?C# - 종료 Application.Run()

Thread messageLoop = new Thread(() => Application.Run()); 
messageLoop.SetApartmentState(ApartmentState.STA); 
messageLoop.Start(); 
//How to terminate thread like on Application.ExitThread() without calling Thread.Abort()? 

미리 감사드립니다.

답변

11

스레드에 대한 참조를 유지하려면 ApplicationContext 클래스를 사용하십시오. 이렇게 :

ApplicationContext threadContext; 

    private void startLoop() { 
     threadContext = new ApplicationContext(); 
     var messageLoop = new Thread(() => Application.Run(threadContext)); 
     messageLoop.Start(); 
    } 

    private void stopLoop() { 
     threadContext.ExitThread(); 
     threadContext = null; 
    } 
+1

이길 때까지 1 초! ;-) –

+1

Hehe, 예제 코드를 테스트하는 데 1 초 이상 걸렸습니다. –

+1

험프, 테스트 schmesting! –