【问题标题】:C#, Constantly monitor battery levelC#,不断监测电池电量
【发布时间】:2013-07-24 11:40:50
【问题描述】:

我正在设计一个依赖于监控计算机电池电量的程序。

这是我正在使用的 C# 代码:

   PowerStatus pw = SystemInformation.PowerStatus;

   if (pw.BatteryLifeRemaining >= 75)
   {
       //Do stuff here
   }

我对@9​​87654323@ 语句的尝试失败,它使用了所有不需要的CPU。

    int i = 1;
    while (i == 1)
    {
        if (pw.BatteryLifeRemaining >= 75)
        {
           //Do stuff here
        }
    }

我如何通过无限循环持续监控它,以便当它达到 75% 时它会执行一些代码。

【问题讨论】:

  • 首先:使用while(true) 而不是你的while,其次Do stuff here 的东西会破坏你的代码,while(true) 不会(当然,如果你在某处有break):)
  • 将监视器移动到不同的线程。预见 a callback to the gui 更新您的 GUI
  • While(true) 不会创建一些无法访问的代码(就像我一样)。 @wud
  • 简而言之,线程就像一个进程。当您在默认线程(GUI 线程)中使用while (true) 时,GUI 将变得无响应。为了防止这种情况发生,您需要创建另一个线程(线程、后台工作者、计时器)来处理 while 循环。唯一的问题是只有 GUI 线程能够更新 GUI。这就是为什么你需要调用正确的 GUI 线程并让它更新它的 GUI...
  • 如果你做一个紧密的循环,除了每秒检查一千次电池电量之外什么都不做,那么你很快就会收敛到零电池电量。

标签: c# monitoring batterylevel


【解决方案1】:

尝试计时器:

public class Monitoring
{
    System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();

    public Monitoring()
    {
        timer1.Interval = 1000; //Period of Tick
        timer1.Tick += timer1_Tick;
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        CheckBatteryStatus(); 
    }
    private void CheckBatteryStatus()
    {
        PowerStatus pw = SystemInformation.PowerStatus;

        if (pw.BatteryLifeRemaining >= 75)
        {
            //Do stuff here
        }
    }
}

更新:

还有另一种方法可以完成您的任务。您可以使用SystemEvents.PowerModeChanged。 调用它并等待更改,监控发生的更改然后做你的事情。

static void SystemEvents_PowerModeChanged(object sender, Microsoft.Win32.PowerModeChangedEventArgs e)
{
    if (e.Mode == Microsoft.Win32.PowerModes.StatusChange)
    {
         if (pw.BatteryLifeRemaining >= 75)
         {
          //Do stuff here
         }
    }
}

【讨论】:

  • 绝对采用“更新”方法。不断轮询是检查电池寿命的一种可怕方法。它有点违背了整个目的。如果是我,我会完全删除答案的第一部分。
  • 这里的密码是什么?
【解决方案2】:

While 循环会导致您的 UI 响应不佳并且应用程序会崩溃。您可以通过多种方式解决此问题。请查看以下代码 sn-p 将满足您的需求。

public delegate void DoAsync();

private void button1_Click(object sender, EventArgs e)
{
   DoAsync async = new DoAsync(GetBatteryDetails);
   async.BeginInvoke(null, null);
}

public void GetBatteryDetails()
{
   int i = 0;
   PowerStatus ps = SystemInformation.PowerStatus;
   while (true)
   {
     if (this.InvokeRequired)
         this.Invoke(new Action(() => this.Text = ps.BatteryLifePercent.ToString() + i.ToString()));
     else
         this.Text = ps.BatteryLifePercent.ToString() + i.ToString();

     i++;
   }
}

【讨论】:

  • @Trontor : 谢谢哥们:)
【解决方案3】:
BatteryChargeStatus.Text =  SystemInformation.PowerStatus.BatteryChargeStatus.ToString(); 
BatteryFullLifetime.Text  = SystemInformation.PowerStatus.BatteryFullLifetime.ToString();
BatteryLifePercent.Text  = SystemInformation.PowerStatus.BatteryLifePercent.ToString();
BatteryLifeRemaining.Text = SystemInformation.PowerStatus.BatteryLifeRemaining.ToString();
PowerLineStatus.Text = SystemInformation.PowerStatus.PowerLineStatus.ToString();

如果你想执行一些操作,只需将这些字符串值转换为整数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多