如何在没有表单时检测应用程序关闭(控制台应用程序) [英] How to detect application close when no form is present (console application)
问题描述
我一直在想这个问题。
我当前想要一个应用程序来检测它何时被关闭。棘手的是,这个应用程序是一个控制台,这意味着没有窗体关闭事件或类似的方式。
我在谷歌上有一个侦察员,真的只是想出了下面的代码只能检测到关闭,注销,而不是特定的应用程序关闭。
ShutdownEvents代码:
Imports Microsoft.Win32
Module Module1
Sub Main()
AddHandler(SystemEvents.SessionEnding),AddressOf ShutDown.OnShuttingdown
AddHandler(SystemEvents.SessionEnded),AddressOf ShutDown.OnShutdown
Console.ReadKey()
End Sub
Public Class ShutDown
Public Shared Sub OnShuttingdown(ByVal sender As Object ,ByVal e As SessionEndingEventArgs)
e.Cancel = True'仅在关闭关闭请求时设置
Console.WriteLine(关闭 - 原因是& e.Reason)
Console.ReadKey()
结束Sub
Public Shared Sub OnShutdown(ByVa l sender As Object,ByVal e As SessionEndedEventArgs)
Console.WriteLine(Shutdown - Reason is& e.Reason)
Console.ReadKey()
End Sub
End Class
End Module
任何帮助将不胜感激。
问候,
您需要创建一个控制台控制处理程序。在 Console
类中没有这样做的功能,但可以调用Windows API函数来完成它。 C#中的一个示例位于 C#中检测进程退出控制台应用程序。
您需要阅读并理解 SetConsoleCtrlHandler API函数。
我写了几篇文章多年前关于使用C#中的Windows控制台API。不幸的是,DevSource似乎已经失去了描述这一点的那个。另外两个可在
http://www.devsource.com/c/a/Using-VS/Working-with-Console-Screen-Buffers-in-NET/
和
http://www.devsource.com/c/a/Using-VS/Console-Input-in-NET/
这些文章中提供的代码的完整源代码可在 http://mischel.com/pubs/consoledotnet.zip 。
I've been wondering about this for a while.
I currently want an application to detect when it is being closed. The tricky thing is, this application is a console one meaning there is no form close event or like-wise.
I had a scout on google and really only came up with the code below which detects only shutting down, logging off and not specifically application closing.
ShutdownEvents Code:
Imports Microsoft.Win32
Module Module1
Sub Main()
AddHandler (SystemEvents.SessionEnding), AddressOf ShutDown.OnShuttingdown
AddHandler (SystemEvents.SessionEnded), AddressOf ShutDown.OnShutdown
Console.ReadKey()
End Sub
Public Class ShutDown
Public Shared Sub OnShuttingdown(ByVal sender As Object, ByVal e As SessionEndingEventArgs)
e.Cancel = True 'only set if you turn down the shutdown request
Console.WriteLine("Shutting down - Reason is " & e.Reason)
Console.ReadKey()
End Sub
Public Shared Sub OnShutdown(ByVal sender As Object, ByVal e As SessionEndedEventArgs)
Console.WriteLine("Shutdown - Reason is " & e.Reason)
Console.ReadKey()
End Sub
End Class
End Module
Any help would be appreciated.
Regards,
You need to create a console control handler. There is no facility for doing that in the Console
class, but you can call Windows API functions to do it. An example in C# is at Detecting Process Exit From Console Application in C#.
You'll want to read and understand the SetConsoleCtrlHandler API function.
I wrote a couple of articles a few years ago about using the Windows Console API from C#. Unfortunately, DevSource seems to have lost the one that describes this. The other two are available at
http://www.devsource.com/c/a/Using-VS/Working-with-Console-Screen-Buffers-in-NET/
and
http://www.devsource.com/c/a/Using-VS/Console-Input-in-NET/
Full source for the code presented in those articles is available at http://mischel.com/pubs/consoledotnet.zip.
这篇关于如何在没有表单时检测应用程序关闭(控制台应用程序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!