Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions WinSyncScroll/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -557,8 +557,71 @@ public void Dispose()
_logger.LogError(e, "Error cancelling the cancellation token source");
}

// Wait for tasks to complete before disposing resources
// Using Task.Wait in Dispose is acceptable as this is a shutdown scenario
// and we need to ensure tasks complete before disposing underlying resources
#pragma warning disable VSTHRD002 // Synchronously waiting is acceptable in Dispose method
try
{
if (_updateMouseHookRectsLoopTask is not null
&& !_updateMouseHookRectsLoopTask.IsCompleted
&& !_updateMouseHookRectsLoopTask.Wait(TimeSpan.FromSeconds(5)))
{
_logger.LogWarning("Update mouse hook rects loop task did not complete within timeout");
}
}
catch (AggregateException ae)
{
ae.Handle(ex =>
{
if (ex is OperationCanceledException)
{
_logger.LogDebug("Update mouse hook rects loop was cancelled");
return true;
}

_logger.LogError(ex, "Error waiting for update mouse hook rects loop task");
return true;
Comment thread
magicxor marked this conversation as resolved.
});
}
catch (Exception e)
{
_logger.LogError(e, "Error disposing update mouse hook rects loop task");
Comment thread
magicxor marked this conversation as resolved.
Outdated
}

try
{
if (_mouseEventProcessingLoopTask is not null
&& !_mouseEventProcessingLoopTask.IsCompleted
&& !_mouseEventProcessingLoopTask.Wait(TimeSpan.FromSeconds(5)))
{
_logger.LogWarning("Mouse event processing loop task did not complete within timeout");
}
}
catch (AggregateException ae)
{
ae.Handle(ex =>
{
if (ex is OperationCanceledException)
{
_logger.LogDebug("Mouse event processing loop was cancelled");
return true;
}

_logger.LogError(ex, "Error waiting for mouse event processing loop task");
return true;
});
}
catch (Exception e)
{
_logger.LogError(e, "Error disposing mouse event processing loop task");
Comment thread
magicxor marked this conversation as resolved.
Outdated
}
Comment thread
magicxor marked this conversation as resolved.
#pragma warning restore VSTHRD002

// Dispose tasks after they have completed
_updateMouseHookRectsLoopTask?.Dispose();
_mouseEventProcessingLoopTask?.Dispose();
Comment thread
magicxor marked this conversation as resolved.

_mouseHook.Dispose();
_cancellationTokenSource.Dispose();
}
Expand Down
Loading