我如何优雅地处理在WinForms应用程序休眠/睡眠模式? [英] How do I gracefully handle hibernate/sleep modes in a winforms application?

查看:226
本文介绍了我如何优雅地处理在WinForms应用程序休眠/睡眠模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是用C#编写的.NET Windows窗体应用程序。

I am writing a windows form application in .net using C#.

我遇到了一个问题,如果我的程序运行时计算机进入睡眠和/或休眠状态(我不知道在这个时候哪一个,或者两个,导致问题),该机器时,再次醒来的程序只是挂起。只有这样,才能将退出它要杀死从任务管理器的进程。

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.

推荐答案

您需要:

using Microsoft.Win32;   

这是code:

And this is the code:

 void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e)
    {
        if(e.Mode == PowerModes.Suspend)
        {
            this.GracefullyHandleSleep();
        }
    }

这是什么我就跟着。

这篇关于我如何优雅地处理在WinForms应用程序休眠/睡眠模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆