2011-09-10 10 views
5

क्या System.Windows.Forms.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

हे, उदाहरण कोड का परीक्षण एक सेकंड से अधिक समय ले लिया। –

+1

हम्फ, परीक्षण schmesting! –

संबंधित मुद्दे