如何检测,当主线程终止? [英] How to detect when main thread terminates?

查看:219
本文介绍了如何检测,当主线程终止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道的:

我想检测时,主线程(进程?)结束,这样我可以保证一定的行动终止前进行。

I would like to detect when a the main thread (process?) terminates so that I can ensure certain actions are performed before it is terminated.

我发现自己:

我找到了事件的 AppDomain.DomainUnload AppDomain.ProcessExit AppDomain.DomainUnload 似乎与非应用程序,如MbUnit的工作。 AppDomain.ProcessExit 似乎与应用工作,但有3秒的时间限制,我真的不喜欢。是否有更多的方法来检测一个AppDomain /进程终止时?

I found the events AppDomain.DomainUnload and AppDomain.ProcessExit. AppDomain.DomainUnload seems to work with non-applications like MbUnit. AppDomain.ProcessExit seems to work with applications but there is a 3 second time limit which I really don't like. Is there more ways to detect when an AppDomain / process terminates?

背景:

我要寻找这样的活动,以确保我的日志persistet当应用程序终止到文件。实际运行记录上使用的生产者 - 消费者模式,其中很可能日志条目可能排队在内存中另一个线程,我需要确保该队列被保存在应用程序终止到文件。

I am looking for such an event to ensure my log is persistet to file when the application terminates. The actual logging runs on another thread using a producer-consumer pattern where it is very likely that log entries might queue up in memory and I need to ensure this queue is saved to file when the application terminates.

还有什么我应该知道的?

更新:

变更上述以反映我已经找到了我自己。我很不满意ProcessExit在3秒时间限制。 MSDN文档不说,虽然,它可以扩展:

Changed the above to reflect what I have found out myself. I am not happy with the 3 second time limit during ProcessExit. The MSDN documentation does say though that it can be extended:

所有的总执行时间   ProcessExit事件处理程序是有限的,   正如总执行时间   所有的终结是在处理有限   关掉。默认为三   秒,这可以由被覆盖   非托管主机。

The total execution time of all ProcessExit event handlers is limited, just as the total execution time of all finalizers is limited at process shutdown. The default is three seconds, which can be overridden by an unmanaged host.

有谁知道如何来覆盖默认?

Does anyone know how to override the default?

更多的想法也非常AP preciated!

More ideas are also highly appreciated!

追问:

我已经发布的后续问题,这

推荐答案

你到底想搞清楚?

  • 当进程终止? (只是因为在AppDomain被卸载不的不一定的意思是,整个过程被终止)
  • 当主线程终止(如果有其他非后台线程,主线程可以终止不终止程序(或应用程序域卸载)
  • When the process terminates? (Just because the AppDomain is unloaded doesn't necessarily mean that the entire process is terminating)
  • When the main thread terminates (If there are other non-background threads, the main thread can terminate without the process terminating (or AppDomain unloading)

因此​​,他们并不完全是一回事。

So they're not quite the same thing.

无论如何,这是一般的危险在所有的有缓存在内存中的日志消息。如果有人关闭电源,会发生什么?或者,如果我终止通过任务管理器的进程?您的所有日志信息都没有了。所以很多时候,你需要缓冲写入日志,以获取消息推到磁盘的马上的。

Anyway, it is generally dangerous to have log messages buffered in memory at all. What happens if someone turns off the power? Or if I terminate your process through Task Manager? All your log messages are gone. So often, you'll want unbuffered writes in your log, to get messages pushed to disk immediately.

不管怎样,另一个(更强大的)办法是,运行记录器本身在非后台线程。这样一来,即使应用程序的其余部分终止时,记录器将没有,所以该过程被保持活着。然后,你只需要设置一些标志的应用程序的其他部分终止时,让记录仪知道,它也应该关闭,一旦它写出所有待处理的日志信息。

Anyway, another (more robust) approach might be to run the logger itself in a non-background thread. That way, even if the rest of the application terminates, the logger won't, so the process is kept alive. Then you just have to set some flag when the rest of the app terminates, to let the logger know that it too should close once it has written out all pending log messages.

这仍然不会救你的情况下,系统断电或有人强行termianates在操作系统级别的过程,但它会处理所有情况下的应用程序正常关闭,带给你无限的时间来执行清理-up行动(因为这个过程实际上没有结束呢,它仍然得到了一个活动线程)

It still won't save you from the case where the system loses power or someone forcibly termianates the process on the OS-level, but it will handle all cases where the application closes normally, and gives you unlimited time to perform clean-up actions (since the process isn't actually terminating yet, it's still got one live thread)

这篇关于如何检测,当主线程终止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆