让一个无形的 C# 程序在关闭/重新启动期间自行关闭?
我正在编写一个程序,在需要之前根本不创建任何表单。问题是,它阻止了自动继续关闭。我看过讨论 关于添加 if
来形成关闭事件以检查是否由于关闭而导致,但正如我所说,我的程序在需要之前根本没有任何形式。
是否有任何事件或其他方法可以让我知道我的程序何时应该自行关闭以允许 Windows 自动关闭? 而且它不是多线程的。
I'm writing a program which creates no forms at all until one is required. The problem is, it's preventing shutdown from continuing automatically. I've seen discussions about adding an if
to form closing events to check if it's due to shutdown, but as I've said, my program is meant to have no forms at all until required.
Is there any event or some other method that will allow me to know when my program should be closing itself to allow for Windows to shut itself down automatically? And it's not multithreaded.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
处理 Microsoft.Win32.SystemEvents.SessionEnding 事件,并使用 System.Environment.HasShutdownStarted 检查是否实际关闭
Handling the
Microsoft.Win32.SystemEvents.SessionEnding
event, and checking if it's an actual shutdown withSystem.Environment.HasShutdownStarted
您始终可以添加一个最小化打开的虚拟表单,任务栏上没有图标 - 它不会有任何视觉影响,但会发送表单关闭事件 - 您可以在其中注意到关闭事件,并可能关闭/停止您的应用程序正在执行的任何其他操作。
You could always add a dummy form which you open minimized, with no icon on the taskbar - it won't have any visual impact, but will be sent the form closing event - where you could note the shutdown event, and presumably shut down/stop whatever else there is that your application is doing.
您可以使用
SystemEvents
类,用于“监听”用户注销或关闭。如果我正确理解文档(并且对 Reflector 的深入研究证实了这一点):
You can use the
SystemEvents
class, to "listen to" users logging out, or shutting down.If I understand the documentation correctly (and a deep study with Reflector confirms this):