I am writing a windows form application in .net using C#.
我正在使用C#在.net中编写一个Windows窗体应用程序。
I am running into a problem that if my program is running when the computer goes into the sleep and/or hibernate state (I am not sure at this time which one, or if both, cause the problem), when the machine wakes up again the program just hangs. The only way to exit out of it is to kill the process from the task manager.
我遇到一个问题,如果我的程序在计算机进入睡眠和/或休眠状态时运行(我不确定此时哪一个,或两者都导致问题),当机器再次唤醒时该程序只是挂起。退出它的唯一方法是从任务管理器中终止进程。
This is, for obvious reasons, not the way I want the program to function. Even if I just shut the program down when it goes into these states, that would be fine, but I am not quite sure how to do this or if there is a more graceful way altogether of handling this.
出于显而易见的原因,这不是我希望程序运行的方式。即使我只是在程序进入这些状态时关闭程序,那也没关系,但我不太清楚如何做到这一点,或者是否有更优雅的方式来处理这个问题。
3 个解决方案
#1
8
You need:
using Microsoft.Win32;
And this is the code:
这是代码:
void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e)
{
if(e.Mode == PowerModes.Suspend)
{
this.GracefullyHandleSleep();
}
}
This is what I went with.
这就是我的用途。
#2
6
Handling those events may be a work-around. But before applying this kind of work-around I'd try to figure out what the application was doing when the OS went into hibernate.
处理这些事件可能是一种解决方法。但在应用这种解决方法之前,我会尝试弄清楚当操作系统进入休眠状态时应用程序正在做什么。
Occurs hanging although application was just idle?
虽然应用程序只是闲置,但发生悬挂?
Is the application doing some kind of low-level work (communication with device drivers or external hardware) that should not be interrupted?
应用程序是否正在进行某种不应中断的低级别工作(与设备驱动程序或外部硬件通信)?
Does the application use some kind of network connection?
应用程序是否使用某种网络连接?
#3
#1
8
You need:
using Microsoft.Win32;
And this is the code:
这是代码:
void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e)
{
if(e.Mode == PowerModes.Suspend)
{
this.GracefullyHandleSleep();
}
}
This is what I went with.
这就是我的用途。
#2
6
Handling those events may be a work-around. But before applying this kind of work-around I'd try to figure out what the application was doing when the OS went into hibernate.
处理这些事件可能是一种解决方法。但在应用这种解决方法之前,我会尝试弄清楚当操作系统进入休眠状态时应用程序正在做什么。
Occurs hanging although application was just idle?
虽然应用程序只是闲置,但发生悬挂?
Is the application doing some kind of low-level work (communication with device drivers or external hardware) that should not be interrupted?
应用程序是否正在进行某种不应中断的低级别工作(与设备驱动程序或外部硬件通信)?
Does the application use some kind of network connection?
应用程序是否使用某种网络连接?