gpt4 book ai didi

C# - 终止 Application.Run()

转载 作者:太空狗 更新时间:2023-10-29 21:06:27 26 4
gpt4 key购买 nike

有没有办法彻底终止通过从另一个线程调用 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# - 终止 Application.Run(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7372947/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com