有没有一种方法可以干净地终止从另一个线程调用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()?

提前致谢!

最佳答案

使用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;
    }

关于c# - C#-终止Application.Run(),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7372947/

10-11 07:53