2011-09-10 45 views
5

Có cách nào để xóa sạch vòng lặp thông điệp được phát hành bằng cách gọi System.Windows.Forms.Application.Run() từ một chuỗi khác không?C# - Chấm dứt 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()? 

Cảm ơn bạn trước!

Trả lời

11

Sử dụng lớp ApplicationContext để giữ tham chiếu đến chuỗi. Như thế này:

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

đánh bại ya 1 giây! ;-) –

+1

Hehe, kiểm tra mã ví dụ mất nhiều thời gian hơn một giây. –

+1

Humph, thử nghiệm schmesting! –

Các vấn đề liên quan