private void updateWindow(object sender, EventArgs e) /// auto update gui on every timer call { // hide taskbar icon if minimized (keep notify icon) if (this.WindowState == FormWindowState.Minimized) { Hide(); /// hide taskbar icon if window is minimized } // battery percent (batChargePercent & batChargeLabel) float batPercent = SystemInformation.PowerStatus.BatteryLifePercent * 100; /// battery percent gets returned as decimal of 1 batChargePercent.Text = ((int)batPercent).ToString() + '%'; batChargeBar.Value = (int)batPercent; /// assign batPercent value to progress bar // log full charge if ((int)batPercent >= 90) { System.IO.File.WriteAllText("log.txt", DateTime.Now.ToString()); /// write current time to file if charge >90% } // compare full charge to now DateTime fullCharge = Convert.ToDateTime(System.IO.File.ReadAllText("log.txt")); /// get time of last full charge TimeSpan sinceCharge = DateTime.Now.Subtract(fullCharge); /// compare to current time for "time since last charge" // charging status (batChargingStatusInfo) PowerLineStatus chargingStatus = SystemInformation.PowerStatus.PowerLineStatus; /// get powerline status if (chargingStatus.ToString() == "Online") /// if else statement is only useful to change the string that gets displayed { string batChargingState = "Charging"; // set gui text batChargingStatusInfo.Text = batChargingState; // set notify icon text batInfoIcon.Text = "Charge: " + ((int)batPercent).ToString() + "%\n" + "Status: " + batChargingState + "\n" + "Last charge: " + TimeUtil.ToHumanTimeString(sinceCharge); } else { // same as before string batChargingState = "Discharging"; batChargingStatusInfo.Text = batChargingState; batInfoIcon.Text = "Charge: " + ((int)batPercent).ToString() + "%\n" + "Status: " + batChargingState + "\n" + "Last charge: " + TimeUtil.ToHumanTimeString(sinceCharge); } timeSinceLastFullInfo.Text = TimeUtil.ToHumanTimeString(sinceCharge); /// put time since last charge on screen ///// to-do: /// run on startup and in notification area only }
/// <summary> /// <para>Returns the current Power status</para> /// </summary> /// <returns></returns> public static TreeNode GetStatus() { TreeNode tnMain = new TreeNode("Power"); tnMain.Nodes.Add("Active power scheme: " + ActivePowerScheme.Name + " (" + ActivePowerScheme.Guid.ToString() + ")"); tnMain.Nodes.Add("Power Line Status: " + PowerLineStatus.ToString()); tnMain.Nodes.Add("Battery charge status: " + BatteryChargeStatus.ToString()); tnMain.Nodes.Add("Battery available: " + BatteryAvailable.ToString()); tnMain.Nodes.Add("Battery full life time: " + BatteryFullLifetime.ToString()); tnMain.Nodes.Add("Battery remaining life time: " + BatteryLifeRemaining.ToString()); tnMain.Nodes.Add("Battery charge percent: " + BatteryLifePercent + "%"); return(tnMain); }
public void loop() { while (true) { PowerLineStatus pls = SystemInformation.PowerStatus.PowerLineStatus; if (pls.ToString() == "Online" && wallpaperEngineStatus == false) { //turn on service wallpaperEngineService.Start(); wallpaperEngineStatus = true; } else if (pls.ToString() == "Offline" && wallpaperEngineStatus == true) { //turn off service and main process processes = Process.GetProcesses().ToList(); //Reload processes wallpaperEngineService.Stop(); processes.Where(x => x.ProcessName == "wallpaper64").FirstOrDefault()?.Kill(); wallpaperEngineStatus = false; } Thread.Sleep(interval); } }