简体   繁体   English

如何跟踪我的应用程序何时意外关闭?

[英]How to track when my application has unexpectedly shut down?

I'm writing an application whose purpose involves a lot of logging of different events. 我正在编写一个应用程序,其目的涉及大量不同事件的记录。 Among those I would also like to have an event that the application was shut down - even if unexpectedly like because of a power loss. 在这些事件中,我还希望发生一个事件,即即使由于断电而意外关闭了该应用程序。

Naturally, when the power goes out I don't get a chance to write anything anywhere. 自然,当电源关闭时,我没有机会在任何地方写任何东西。 So my idea was to continuously write a timestamp in some known location (say, once per minute), and when the application was next run, it could determine the approximate time of the unexpected shutdown. 因此,我的想法是在某个已知位置连续写入时间戳(例如,每分钟一次),然后在下次运行该应用程序时,可以确定意外关闭的大概时间。 A precision of 1 minute would be acceptable for me. 1分钟的精度对我来说是可以接受的。

However I'm worried that caching at the OS and disk level might interfere with this approach. 但是,我担心在操作系统和磁盘级别进行缓存可能会干扰这种方法。 Is there a better way or if not - how to make sure that the data I just wrote is REALLY written out to the physical medium? 有没有更好的方法,或者是否有更好的方法-如何确保我刚刚写入的数据真正被写到物理介质上?

Added: Oh, almost forgot the buzzword line: Windows XP and above; 补充:哦,差点忘记了流行语:Windows XP及更高版本; .NET 3.5; .NET 3.5; C#. C#。

Unexpected shutdowns are logged in the system event log. 意外关闭将记录在系统事件日志中。

When your application shuts down cleanly, write it to your logfile. 当您的应用程序完全关闭时,请将其写入日志文件。 Next time your application starts check if your application was shut down the way it supposed to, otherwise check the system event log. 下次您的应用程序启动时,请检查您的应用程序是否以应有的方式关闭,否则请检查系统事件日志。

Edit: removed the incorrect answer (due to not reading the question properly) 编辑:删除了错误的答案(由于未正确阅读问题)

You could use the FlushFileBuffers method, but that will still only write it to the device, you still can't get the actual drive to write it (as far as I know). 您可以使用FlushFileBuffers方法,但这仍然只会将其写入设备,您仍然无法获得实际的驱动器来写入它(据我所知)。
http://www.pinvoke.net/default.aspx/kernel32/FlushFileBuffers.html?diff=y http://www.pinvoke.net/default.aspx/kernel32/FlushFileBuffers.html?diff=y

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在第一个关闭后,如何声明第二个新的Application对象? - How can I declare a second, new Application object after the first has shut down? 我如何从另一个表格关闭我的应用程序? - How do i close shut down my application from another Form? 如何在.NET中检测哪个线程阻止应用程序关闭 - How to detect which thread is holding the application from shut down in .NET 如何使WPF崩溃的应用程序立即关闭(立即是关键字)? - How to make WPF crashing application shut down immediately ( immediately is the keyword)? 如何通知正在运行的线程该应用程序将在.net中关闭? - How to notify the running thread that application is going to shut down in .net? 如果程序意外关闭,IDisposable对象是否会被丢弃? - Do IDisposable objects get disposed of if the program is shut down unexpectedly? 如何在“开始/”关闭“按钮顶部出现”安装更新并关闭计算机“屏蔽图标时以编程方式检测? - How to detect programmatically when the “Install Updates and shut down the computer” shield icon is present on top of the Start / “Shut down” button? 应用程序对象被关闭异常 - Application object is being shut down exception 不活动的应用程序池在线程上关闭 - Inactive Application Pool shut down on a thread 当我关闭C#应用程序时,DLL中的System.MissingMethodException - System.MissingMethodException in a dll when I shut down c# application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM