Improved thread-safety for system components.

This commit is contained in:
dbuechel 2019-08-16 15:45:56 +02:00
parent 46a3452d93
commit f25516e858
2 changed files with 48 additions and 32 deletions

View file

@ -20,7 +20,7 @@ namespace SafeExamBrowser.SystemComponents
{ {
public class PowerSupply : ISystemComponent<ISystemPowerSupplyControl> public class PowerSupply : ISystemComponent<ISystemPowerSupplyControl>
{ {
private const int TWO_SECONDS = 2000; private readonly object @lock = new object();
private bool infoShown, warningShown; private bool infoShown, warningShown;
private ILogger logger; private ILogger logger;
@ -37,6 +37,8 @@ namespace SafeExamBrowser.SystemComponents
public void Initialize() public void Initialize()
{ {
const int TWO_SECONDS = 2000;
timer = new Timer(TWO_SECONDS); timer = new Timer(TWO_SECONDS);
timer.Elapsed += Timer_Elapsed; timer.Elapsed += Timer_Elapsed;
timer.AutoReset = true; timer.AutoReset = true;
@ -47,7 +49,11 @@ namespace SafeExamBrowser.SystemComponents
public void Register(ISystemPowerSupplyControl control) public void Register(ISystemPowerSupplyControl control)
{ {
controls.Add(control); lock (@lock)
{
controls.Add(control);
}
UpdateControls(); UpdateControls();
} }
@ -72,37 +78,40 @@ namespace SafeExamBrowser.SystemComponents
private void UpdateControls() private void UpdateControls()
{ {
var charge = SystemInformation.PowerStatus.BatteryLifePercent; lock (@lock)
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)
{ {
tooltip = text.Get(percentage == 100 ? TextKey.SystemControl_BatteryCharged : TextKey.SystemControl_BatteryCharging); var charge = SystemInformation.PowerStatus.BatteryLifePercent;
infoShown = false; var percentage = Math.Round(charge * 100);
warningShown = false; var status = charge <= 0.4 ? (charge <= 0.2 ? BatteryChargeStatus.Critical : BatteryChargeStatus.Low) : BatteryChargeStatus.Okay;
} var online = SystemInformation.PowerStatus.PowerLineStatus == PowerLineStatus.Online;
else var tooltip = string.Empty;
{
var hours = SystemInformation.PowerStatus.BatteryLifeRemaining / 3600;
var minutes = (SystemInformation.PowerStatus.BatteryLifeRemaining - (hours * 3600)) / 60;
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); HandleBatteryStatus(status);
tooltip = tooltip.Replace("%%HOURS%%", hours.ToString());
tooltip = tooltip.Replace("%%MINUTES%%", minutes.ToString());
}
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) tooltip = tooltip.Replace("%%CHARGE%%", percentage.ToString());
{
control.SetBatteryCharge(charge, status); foreach (var control in controls)
control.SetPowerGridConnection(online); {
control.SetInformation(tooltip); control.SetBatteryCharge(charge, status);
control.SetPowerGridConnection(online);
control.SetInformation(tooltip);
}
} }
} }

View file

@ -22,7 +22,6 @@ namespace SafeExamBrowser.SystemComponents
{ {
public class WirelessNetwork : ISystemComponent<ISystemWirelessNetworkControl> public class WirelessNetwork : ISystemComponent<ISystemWirelessNetworkControl>
{ {
private const int FIVE_SECONDS = 5000;
private readonly object @lock = new object(); private readonly object @lock = new object();
private List<ISystemWirelessNetworkControl> controls; private List<ISystemWirelessNetworkControl> controls;
@ -73,7 +72,10 @@ namespace SafeExamBrowser.SystemComponents
control.SetInformation(text.Get(TextKey.SystemControl_WirelessNotAvailable)); control.SetInformation(text.Get(TextKey.SystemControl_WirelessNotAvailable));
} }
controls.Add(control); lock (@lock)
{
controls.Add(control);
}
if (hasWifiAdapter) if (hasWifiAdapter)
{ {
@ -131,9 +133,12 @@ namespace SafeExamBrowser.SystemComponents
logger.Error($"Failed to connect to wireless network '{name}!'"); 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(); UpdateAvailableNetworks();
@ -227,6 +232,8 @@ namespace SafeExamBrowser.SystemComponents
private void StartTimer() private void StartTimer()
{ {
const int FIVE_SECONDS = 5000;
timer = new Timer(FIVE_SECONDS); timer = new Timer(FIVE_SECONDS);
timer.Elapsed += Timer_Elapsed; timer.Elapsed += Timer_Elapsed;
timer.AutoReset = true; timer.AutoReset = true;