1
0
mirror of https://github.com/DarthAffe/RGB.NET.git synced 2025-12-13 10:08:31 +00:00

Merge remote-tracking branch 'origin/Core/ThreadSafety' into Development

This commit is contained in:
SpoinkyNL 2020-01-12 16:48:18 +01:00
commit 9958da79d8

View File

@ -14,6 +14,8 @@ namespace RGB.NET.Core
{ {
#region Properties & Fields #region Properties & Fields
private object _lock = new object();
private CancellationTokenSource _updateTokenSource; private CancellationTokenSource _updateTokenSource;
private CancellationToken _updateToken; private CancellationToken _updateToken;
private Task _updateTask; private Task _updateTask;
@ -59,25 +61,32 @@ namespace RGB.NET.Core
/// </summary> /// </summary>
public void Start() public void Start()
{ {
if (_updateTask == null) lock (_lock)
{ {
_updateTokenSource?.Dispose(); if (_updateTask == null)
_updateTokenSource = new CancellationTokenSource(); {
_updateTask = Task.Factory.StartNew(UpdateLoop, (_updateToken = _updateTokenSource.Token), TaskCreationOptions.LongRunning, TaskScheduler.Default); _updateTokenSource?.Dispose();
_updateTokenSource = new CancellationTokenSource();
_updateTask = Task.Factory.StartNew(UpdateLoop, (_updateToken = _updateTokenSource.Token), TaskCreationOptions.LongRunning, TaskScheduler.Default);
}
} }
} }
/// <summary> /// <summary>
/// Stops the trigger if running, causing it to stop performing updates. /// Stops the trigger if running, causing it to stop performing updates.
/// </summary> /// </summary>
public async void Stop() public void Stop()
{ {
if (_updateTask != null) lock (_lock)
{ {
_updateTokenSource.Cancel(); if (_updateTask != null)
await _updateTask; {
_updateTask.Dispose(); _updateTokenSource.Cancel();
_updateTask = null; // ReSharper disable once MethodSupportsCancellation
_updateTask.Wait();
_updateTask.Dispose();
_updateTask = null;
}
} }
} }