为什么我的应用程序在注销/关闭时没有关闭(c#/.net winforms)?

发布于 2024-07-13 16:34:22 字数 365 浏览 11 评论 0原文

当我注销/关闭时,我的 winforms 应用程序没有很好地关闭。 我有一个主窗体,其 Closing 事件已正确触发,但必须有其他东西保留我的应用程序。 如果我检查 Application.OpenForms ,就会发现只有一个主窗体。

棘手的一点(也是问题可能所在)是,我的应用程序使用 ShellWindows 挂接到 Internet Explorer,并且在 IE 事件触发时偶尔会打开表单。 在打开和关闭其中一个或多个表单后,我的应用程序在关闭时停止关闭。

我想我正在清理所有表单对象等并适当地调用 FinalReleaseComObject() ,但我想有些地方有引用使我的进程保持打开状态。 有什么方法可以找出阻止我的应用程序正常关闭的原因吗?

My winforms app isn't shutting down nicely when I log off/shutdown. I have a main form, whose Closing event is fired correctly, but there must be something else keeping my application around. If I check Application.OpenForms there's just my one main form.

The tricky bit, and where the problem probably lies, is that my application uses ShellWindows to hook into Internet Explorer, and occassionally opens up forms when IE events fire. It's after one or more of these forms has been opened and closed that my app stops closing on shutdown.

I think I'm cleaning up all form objects etc and calling FinalReleaseComObject() appropriately, but I guess there are references somewhere that are holding my process open. Is there any way to work out what it is that's stopping my app from closing gracefully?

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

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

发布评论

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

评论(3

唐婉 2024-07-20 16:34:22

如果有正在运行的线程尚未设置为后台,应用程序也将保持打开状态。 如果您要创建自己的任何线程,请确保它们正确终止。

如果线程完成并不重要,请设置 IsBackground 为真。 您还可以在线程上调用 Abort 来(某种程度上)强制终止它。

An application will also stay open if there are threads running that have not been set to background. If you are creating any of your own threads, make sure that they are terminating appropriately.

If it is not critical that the thread finishes, set IsBackground to true. You can also call Abort on a thread to (somewhat)forcibly kill it.

半衬遮猫 2024-07-20 16:34:22

最可能的原因是您有一个后台线程挂在周围,当您的应用程序的主窗口关闭时,该后台线程没有被关闭。 根据您的设置和框架版本,后台线程可以在主线程终止时使应用程序保持活动状态。

在关闭期间,窗口通常通过向进程上的主窗口发送 WM_QUIT 来要求每个正在运行的应用程序终止。 WinForms 会很乐意使用此消息来关闭主窗口,但如果留下任何后台线程,则实际进程可以继续。

The most likely cause is that you have a background thread hanging around that is not being closed when the main window of your application is closed. Depending on your settings and framework version background threads can keep an application alive when the main thread is terminated.

During shutdown windows asks every running app to terminate usually by sending a WM_QUIT to the main window on a process. WinForms will happily use this message to shutdown the main window but if any background threads are left the actual process could continue.

盗琴音 2024-07-20 16:34:22

这是一种非常丑陋的方法,但如果您只想杀死任何挂起的线程,您可以使用 System.Diagnostics.Process.GetCurrentProcess.Threads 获取应用程序中运行的所有线程,然后枚举它们并对其调用 Thread.Join() 或 Thread.Abort()。

只需确保不在您正在使用的主 (UI) 线程(接收 Closing 事件的线程)上调用 .Abort() 即可。 因此,请确保检查当前线程 (System.Threading.Thread) 不是您要中止的线程。

This is a really ugly way to do this, but if all you want to do is kill any thread that's hanging around, you can get all the threads running in your application with System.Diagnostics.Process.GetCurrentProcess.Threads, and then enumerate through them and call Thread.Join() or Thread.Abort() on them.

Just make sure to NOT call .Abort() on the main (UI) thread that you're working from (the one that receives the Closing event). So make sure to check that your current thread (System.Threading.Thread) is not the one you're aborting.

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