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>
{
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;
@ -46,8 +48,12 @@ namespace SafeExamBrowser.SystemComponents
}
public void Register(ISystemPowerSupplyControl control)
{
lock (@lock)
{
controls.Add(control);
}
UpdateControls();
}
@ -71,6 +77,8 @@ namespace SafeExamBrowser.SystemComponents
}
private void UpdateControls()
{
lock (@lock)
{
var charge = SystemInformation.PowerStatus.BatteryLifePercent;
var percentage = Math.Round(charge * 100);
@ -105,6 +113,7 @@ namespace SafeExamBrowser.SystemComponents
control.SetInformation(tooltip);
}
}
}
private void HandleBatteryStatus(BatteryChargeStatus status)
{

View file

@ -22,7 +22,6 @@ namespace SafeExamBrowser.SystemComponents
{
public class WirelessNetwork : ISystemComponent<ISystemWirelessNetworkControl>
{
private const int FIVE_SECONDS = 5000;
private readonly object @lock = new object();
private List<ISystemWirelessNetworkControl> controls;
@ -73,7 +72,10 @@ namespace SafeExamBrowser.SystemComponents
control.SetInformation(text.Get(TextKey.SystemControl_WirelessNotAvailable));
}
lock (@lock)
{
controls.Add(control);
}
if (hasWifiAdapter)
{
@ -131,10 +133,13 @@ namespace SafeExamBrowser.SystemComponents
logger.Error($"Failed to connect to wireless network '{name}!'");
}
lock (@lock)
{
foreach (var control in controls)
{
control.IsConnecting = false;
}
}
UpdateAvailableNetworks();
UpdateControls();
@ -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;