[英].NET Console App - can it detect its own imminent termination due to system shutdown?
I have a small console app that runs continuously on an Azure Windows 10 VM , doing some calculations.我有一个小型控制台应用程序,它在Azure Windows 10 VM上连续运行,进行一些计算。
But what I would like would be if the job could detect when the VM is about to reboot, or when it itself is about to terminate, and write something to ApplicationInsights .但我想要的是,如果作业可以检测到 VM 何时将要重启,或者它本身何时将终止,并向ApplicationInsights写入一些内容。
Can a .NET console app, running under Windows 10, detect its own termination (due to a system shut down, or for other reasons) in time to write some telemetry?在 Windows 10 下运行的 .NET 控制台应用程序能否及时检测到自己的终止(由于系统关闭或其他原因)以编写一些遥测数据?
I've set up the following events (an example of one handler is shown), but they never seem to produce any telemetry, which makes me think there's something I don't "get" about how a shutdown happens.我已经设置了以下事件(显示了一个处理程序的示例),但它们似乎从未产生任何遥测数据,这让我觉得有些事情我没有“了解”关机是如何发生的。 Perhaps there isn't time for the telemetry to be sent.也许没有时间发送遥测数据。
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
AppDomain.CurrentDomain.ProcessExit += CurrentDomain_ProcessExit;
Process.GetCurrentProcess().Exited += Program_Exited;
}
private static void Program_Exited(object? sender, EventArgs e)
{
telemetryClient?.TrackEvent("ProgramEnding");
}
I searched, of course.当然,我搜索过。 I found a reference for the Microsoft.Win32.SystemEvents class, which has events for SessionEnding , although it looks as if I'd need to generate a hidden form in order to be notified of that event (ugh).我找到了Microsoft.Win32.SystemEvents class 的参考,其中包含SessionEnding的事件,尽管看起来我需要生成隐藏表单才能收到该事件的通知(呃)。
Is that the direction I need to head in?那是我需要前进的方向吗? And, is it even possible for a console app to detect its own termination in time to respond?而且,控制台应用程序甚至有可能及时检测到自己的终止以做出响应吗?
I found a reference for the Microsoft.Win32.SystemEvents class, which has events for SessionEnding, although it looks as if I'd need to generate a hidden form in order to be notified of that event我找到了 Microsoft.Win32.SystemEvents class 的参考,其中包含 SessionEnding 事件,尽管看起来我需要生成一个隐藏表单才能收到该事件的通知
More specifically, you need a message pump to receive the WM_ENDSESSION
message from the OS.更具体地说,您需要一个消息泵来接收来自操作系统的WM_ENDSESSION
消息。 You can do that as you wish, whether by using a hidden form with the managed API or by manually calling GetMessage
/ DispatchMessage
on a window you create manually with native API.您可以按照自己的意愿执行此操作,无论是使用带有托管 API 的隐藏表单,还是通过在您使用本机 API 手动创建的 window 上手动调用GetMessage
/ DispatchMessage
。
is it even possible for a console app to detect its own termination in time to respond?控制台应用程序甚至有可能及时检测到自己的终止以做出响应吗?
The way shut down works is that you get the above message, and while you're processing it (either directly or through the SessionEnding
event handler) the system will wait for you and eventually show that "programs still need to close" message, and the user can choose to allow more time or not.关闭的工作方式是您收到上述消息,并且在您处理它时(直接或通过SessionEnding
事件处理程序)系统将等待您并最终显示“程序仍然需要关闭”消息,并且用户可以选择是否允许更多时间。 Unless it's a critical shutdown in which case you only get a short time window.除非它是严重关机,在这种情况下你只会得到很短的时间 window。
So yes, you should have plenty of time to do any sane shut down, even including registering for a restart of your application when Windows boots up again.所以是的,您应该有足够的时间进行任何正常的关闭,甚至包括在 Windows 再次启动时注册重新启动您的应用程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.