diff --git a/SafeExamBrowser.SystemComponents/PowerSupply.cs b/SafeExamBrowser.SystemComponents/PowerSupply.cs index f8804e92..f7087731 100644 --- a/SafeExamBrowser.SystemComponents/PowerSupply.cs +++ b/SafeExamBrowser.SystemComponents/PowerSupply.cs @@ -20,7 +20,7 @@ namespace SafeExamBrowser.SystemComponents { public class PowerSupply : ISystemComponent { - private const int TWO_SECONDS = 2000; + private readonly object @lock = new object(); private bool infoShown, warningShown; private ILogger logger; @@ -37,6 +37,8 @@ namespace SafeExamBrowser.SystemComponents public void Initialize() { + const int TWO_SECONDS = 2000; + timer = new Timer(TWO_SECONDS); timer.Elapsed += Timer_Elapsed; timer.AutoReset = true; @@ -47,7 +49,11 @@ namespace SafeExamBrowser.SystemComponents public void Register(ISystemPowerSupplyControl control) { - controls.Add(control); + lock (@lock) + { + controls.Add(control); + } + UpdateControls(); } @@ -72,37 +78,40 @@ namespace SafeExamBrowser.SystemComponents private void UpdateControls() { - var charge = SystemInformation.PowerStatus.BatteryLifePercent; - var percentage = Math.Round(charge * 100); - var status = charge <= 0.4 ? (charge <= 0.2 ? BatteryChargeStatus.Critical : BatteryChargeStatus.Low) : BatteryChargeStatus.Okay; - var online = SystemInformation.PowerStatus.PowerLineStatus == PowerLineStatus.Online; - var tooltip = string.Empty; - - if (online) + lock (@lock) { - tooltip = text.Get(percentage == 100 ? TextKey.SystemControl_BatteryCharged : TextKey.SystemControl_BatteryCharging); - infoShown = false; - warningShown = false; - } - else - { - var hours = SystemInformation.PowerStatus.BatteryLifeRemaining / 3600; - var minutes = (SystemInformation.PowerStatus.BatteryLifeRemaining - (hours * 3600)) / 60; + var charge = SystemInformation.PowerStatus.BatteryLifePercent; + var percentage = Math.Round(charge * 100); + var status = charge <= 0.4 ? (charge <= 0.2 ? BatteryChargeStatus.Critical : BatteryChargeStatus.Low) : BatteryChargeStatus.Okay; + var online = SystemInformation.PowerStatus.PowerLineStatus == PowerLineStatus.Online; + var tooltip = string.Empty; - HandleBatteryStatus(status); + if (online) + { + tooltip = text.Get(percentage == 100 ? TextKey.SystemControl_BatteryCharged : TextKey.SystemControl_BatteryCharging); + infoShown = false; + warningShown = false; + } + else + { + var hours = SystemInformation.PowerStatus.BatteryLifeRemaining / 3600; + var minutes = (SystemInformation.PowerStatus.BatteryLifeRemaining - (hours * 3600)) / 60; - tooltip = text.Get(TextKey.SystemControl_BatteryRemainingCharge); - tooltip = tooltip.Replace("%%HOURS%%", hours.ToString()); - tooltip = tooltip.Replace("%%MINUTES%%", minutes.ToString()); - } + HandleBatteryStatus(status); - tooltip = tooltip.Replace("%%CHARGE%%", percentage.ToString()); + tooltip = text.Get(TextKey.SystemControl_BatteryRemainingCharge); + tooltip = tooltip.Replace("%%HOURS%%", hours.ToString()); + tooltip = tooltip.Replace("%%MINUTES%%", minutes.ToString()); + } - foreach (var control in controls) - { - control.SetBatteryCharge(charge, status); - control.SetPowerGridConnection(online); - control.SetInformation(tooltip); + tooltip = tooltip.Replace("%%CHARGE%%", percentage.ToString()); + + foreach (var control in controls) + { + control.SetBatteryCharge(charge, status); + control.SetPowerGridConnection(online); + control.SetInformation(tooltip); + } } } diff --git a/SafeExamBrowser.SystemComponents/WirelessNetwork.cs b/SafeExamBrowser.SystemComponents/WirelessNetwork.cs index 2fc82bb1..0e9e25b0 100644 --- a/SafeExamBrowser.SystemComponents/WirelessNetwork.cs +++ b/SafeExamBrowser.SystemComponents/WirelessNetwork.cs @@ -22,7 +22,6 @@ namespace SafeExamBrowser.SystemComponents { public class WirelessNetwork : ISystemComponent { - private const int FIVE_SECONDS = 5000; private readonly object @lock = new object(); private List controls; @@ -73,7 +72,10 @@ namespace SafeExamBrowser.SystemComponents control.SetInformation(text.Get(TextKey.SystemControl_WirelessNotAvailable)); } - controls.Add(control); + lock (@lock) + { + controls.Add(control); + } if (hasWifiAdapter) { @@ -131,9 +133,12 @@ namespace SafeExamBrowser.SystemComponents logger.Error($"Failed to connect to wireless network '{name}!'"); } - foreach (var control in controls) + lock (@lock) { - control.IsConnecting = false; + foreach (var control in controls) + { + control.IsConnecting = false; + } } UpdateAvailableNetworks(); @@ -227,6 +232,8 @@ namespace SafeExamBrowser.SystemComponents private void StartTimer() { + const int FIVE_SECONDS = 5000; + timer = new Timer(FIVE_SECONDS); timer.Elapsed += Timer_Elapsed; timer.AutoReset = true;