2011-09-10 12 views
5

C'è un modo per terminare in modo pulito un ciclo di messaggi emesso chiamando System.Windows.Forms.Application.Run() da un altro thread?C# - Terminazione 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()? 

Grazie in anticipo!

risposta

11

Utilizzare la classe ApplicationContext per mantenere un riferimento al thread. In questo modo:

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

battere per 1 secondo! ;-) –

+1

Hehe, testare il codice di esempio ha richiesto più di un secondo. –

+1

Humph, prova schmesting! –

Problemi correlati