C# - 在PowerStatus上更改事件 [英] C# - Raise event on PowerStatus change

查看:200
本文介绍了C# - 在PowerStatus上更改事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个需要处于安全状态的应用程序,所以我想在后台跟踪计算机的电源状态。如果电池电量(如果有的话)低或很重要,我不允许用户继续使用该应用程序并正常退出。



首先,我令人惊讶的是,为了检测这个变化,没有这样的事件存在。你需要经常手动检查PowerStatus。



所以,我已经创建了一个包装器,像这样:

 使用系统; 
使用System.Windows.Forms;

命名空间MyApp
{
内部静态类BatteryManagement
{
//
内部静态事件EventHandler< EventArgs>改变;

//
private static bool _started;
private static System.Threading.Timer _timer;
private static PowerStatus _previousPowerStatus;

internal static void Start()
{
if(!_started)
{
_started = true;
ManageBatteryLevel();
}
}

内部静态void Stop()
{
if(_started)
{
if(_timer! = null)
{
_timer.Dispose();
_timer = null;
}

_started = false;
}
}

private static void ManageBatteryLevel()
{
_previousPowerStatus = new PowerStatus();

TimeSpan dueTime = new TimeSpan(0,0,0); //现在开始
TimeSpan period = new TimeSpan(0,1,0); //每1分钟运行

//设置一个计时器,每个周期启动OnBatteryLevelChange方法
_timer = new System.Threading.Timer(OnBatteryLevelChange,null,dueTime,period);
}

private static void OnBatteryLevelChange(Object stateInfo)
{
PowerStatus powerStatus = new PowerStatus();

如果(!_previousPowerStatus.Equals(powerStatus))
{
//确保在升级事件之前电池电量是最新的
_previousPowerStatus = powerStatus;

if(Changed!= null)
{
已更改(null,EventArgs.Empty);
}
}
}
}
}

但是不起作用,因为PowerStatus没有任何公共构造函数,而且我无法存储上一个状态的结果...



管理这个?



谢谢...

解决方案

其实有,它称为 SystemEvents .PowerModeChanged



如果 PowerModeChangedEventArgs 有一个模式 StatusChange ,表示电池状态已更改。

  static void SystemEvents_PowerModeChanged(object sender,Microsoft.Win32.PowerModeChangedEventArgs e)
{
if(e.Mode == Microsoft.Win32.PowerModes.StatusChange)
{
//检查状态是什么,并采取行动ngly
}
}

本教程也可能有用: p>

http://netcode.ru/dotnet/?lang=&katID=30&skatID=277&artID=7643


I've created an application that need to be in a safe state and so I want to follow the power status of the computer in background. If the battery level (if any) is low or critical, I wouldn't allow the user to continue using the application and quit all properly.

First of all, I'm astonished that no such event exists in order to detect the change. You need always to check the PowerStatus manually.

So, I've created a wrapper around it, something like this :

using System;
using System.Windows.Forms;

namespace MyApp
{
    internal static class BatteryManagement
    {
        //
        internal static event EventHandler<EventArgs> Changed;

        //
        private static bool _started;
        private static System.Threading.Timer _timer;
        private static PowerStatus _previousPowerStatus;

        internal static void Start()
        {
            if (!_started)
            {
                _started = true;
                ManageBatteryLevel();
            }
        }

        internal static void Stop()
        {
            if (_started)
            {
                if(_timer != null)
                {
                    _timer.Dispose();
                    _timer = null;
                }

                _started = false;
            }
        }

        private static void ManageBatteryLevel()
        {
            _previousPowerStatus = new PowerStatus();

            TimeSpan dueTime = new TimeSpan(0, 0, 0); // Start right now
            TimeSpan period = new TimeSpan(0, 1, 0); // Run every 1 minute

            // Setting a timer that launch every period the OnBatteryLevelChange method
            _timer = new System.Threading.Timer(OnBatteryLevelChange, null, dueTime, period);
        }

        private static void OnBatteryLevelChange(Object stateInfo)
        {
            PowerStatus powerStatus = new PowerStatus();

            if (!_previousPowerStatus.Equals(powerStatus))
            {
                // Ensure battery level is up to date before raising event
                _previousPowerStatus = powerStatus;

                if (Changed != null)
                {
                    Changed(null, EventArgs.Empty);
                }
            }
        }
    }
}

But doesn't work because PowerStatus hasn't any public constructor and I can't store the result of the previous status...

How can I manage this ?

Thanks ...

解决方案

Actually there is, its called the SystemEvents.PowerModeChanged

If the PowerModeChangedEventArgs has a Mode of StatusChange, it means the status of the battery has changed.

static void SystemEvents_PowerModeChanged(object sender, Microsoft.Win32.PowerModeChangedEventArgs e)
{
    if (e.Mode == Microsoft.Win32.PowerModes.StatusChange)
    {
         // Check what the status is and act accordingly
    }
}

This tutorial may also be of use:

http://netcode.ru/dotnet/?lang=&katID=30&skatID=277&artID=7643

这篇关于C# - 在PowerStatus上更改事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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