Application.Run() 导致应用程序挂起

发布于 2024-10-08 20:07:37 字数 359 浏览 0 评论 0 原文

当我启动没有特定表单的应用程序时,例如

Application.Run();

然后我创建表单,在用户关闭它后,进程就会挂起。

这发生在 Visual Studio 之外。

我尝试将 Application.Exit() 和/或 Application.ExitThread() 放入表单的 Form_Closing 事件中,但它仍然挂起。

编辑:不幸的是,使用自定义 ApplicationContext 不起作用。

仅供参考,我没有使用任何线程,也没有使用BackgroundWorkers。

When I start application without specific form e.g.

Application.Run();

And then I create form, after user closes it, the process just hangs.

This happens outside of Visual Studio.

I tried to put Application.Exit() and/or Application.ExitThread() in form's Form_Closing event, but it still hangs.

Edit: Using custom ApplicationContext unfortunately doesn't work.

FYI, I'm not using any threads nor BackgroundWorkers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦忆晨望 2024-10-15 20:07:37

使用“工具”+“附加到进程”将调试器附加到挂起的进程。调试+全部破坏。 Debug + Windows + Threads,双击主线程,查看其调用堆栈,看看它在做什么。如果这没有帮助,请在您的问题中发布堆栈跟踪。

Use Tools + Attach to Process to attach the debugger to the hung process. Debug + Break All. Debug + Windows + Threads, double-click the Main thread and look at its call stack to see what it is doing. Post the stack trace in your question if that doesn't help.

莫言歌 2024-10-15 20:07:37

来自无参数 Application.Run() 的 MSDN:

大多数 Windows 窗体开发人员不会
需要使用这个版本的
方法。您应该使用运行(表单)
重载来启动应用程序
主窗体,以便应用程序
当主窗体是时终止
关闭。对于所有其他情况,请使用
Run(ApplicationContext) 重载,
它支持提供一个
ApplicationContext 对象以获得更好的效果
控制生命周期
应用程序。

因此,简短的回答是,尝试指定一个 ApplicationContext 对象,或者只是 Run() 您正在创建和显示的表单。也许一个更具体的例子来说明为什么您试图打开消息循环而不将其绑定到表单会有所帮助。您要打开多个表格吗?这是像闪屏或登录这样的介绍形式吗?

From the MSDN for the parameterless Application.Run():

Most Windows Forms developers will not
need to use this version of the
method. You should use the Run(Form)
overload to start an application with
a main form, so that the application
terminates when the main form is
closed. For all other situations, use
the Run(ApplicationContext) overload,
which supports supplying an
ApplicationContext object for better
control over the lifetime of the
application.

So, short answer, try specifying an ApplicationContext object, or just Run() the form you are creating and showing. Maybe a more concrete example of why you're trying to open the message loop without tying it to a form would help. Are you opening several forms? Is this an introductory form like a splash screen or a login?

黒涩兲箜 2024-10-15 20:07:37

尝试使用 Application.Run(new MyForm()) 的重载,尽管 Application.Exit() 应该按照 此处

Try using the overload of Application.Run(new MyForm()) instead, though Application.Exit() should be working for you as described here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文