Skip to content

Refactor: Extract duplicated task waiting logic into helper method#21

Merged
magicxor merged 3 commits intofeature/wait-tasks-before-disposefrom
copilot/sub-pr-20
Nov 24, 2025
Merged

Refactor: Extract duplicated task waiting logic into helper method#21
magicxor merged 3 commits intofeature/wait-tasks-before-disposefrom
copilot/sub-pr-20

Conversation

Copy link
Contributor

Copilot AI commented Nov 24, 2025

Addresses code duplication in MainViewModel.Dispose() where two nearly identical try-catch blocks wait for tasks to complete before disposal.

Changes

  • Added WaitForTaskCompletion() helper method to encapsulate task waiting, timeout handling, and exception logging
  • Replaced 55 lines of duplicated code with two method calls
  • Moved #pragma warning disable VSTHRD002 into the helper method to suppress warnings at the appropriate scope

Before/After

Before:

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) { /* ... */ }
catch (Exception e) { /* ... */ }

// Duplicate block for _mouseEventProcessingLoopTask

After:

WaitForTaskCompletion(_updateMouseHookRectsLoopTask, "Update mouse hook rects loop task", TimeSpan.FromSeconds(5));
WaitForTaskCompletion(_mouseEventProcessingLoopTask, "Mouse event processing loop task", TimeSpan.FromSeconds(5));

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

…er method

Co-authored-by: magicxor <8275793+magicxor@users.noreply.github.com>
Copilot AI changed the title [WIP] Address feedback on task completion before resource disposal Refactor: Extract duplicated task waiting logic into helper method Nov 24, 2025
Copilot AI requested a review from magicxor November 24, 2025 00:58
@magicxor magicxor marked this pull request as ready for review November 24, 2025 01:00
Copilot AI review requested due to automatic review settings November 24, 2025 01:00
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR successfully refactors the MainViewModel.Dispose() method by extracting duplicated task waiting logic into a reusable helper method, reducing code duplication from 55 lines to 2 method calls.

Key Changes:

  • Introduced WaitForTaskCompletion() private helper method to encapsulate task waiting, timeout handling, and exception logging
  • Refactored Dispose() to use the helper method for both task waiting operations
  • Moved #pragma warning disable VSTHRD002 directive into the helper method scope

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@magicxor magicxor merged commit 744e1b8 into feature/wait-tasks-before-dispose Nov 24, 2025
3 checks passed
@magicxor magicxor deleted the copilot/sub-pr-20 branch November 24, 2025 01:06
magicxor added a commit that referenced this pull request Nov 24, 2025
* Ensures task completion before resource disposal

Waits for mouse hook and event processing tasks to complete
before disposing resources during shutdown. This prevents
potential resource contention and ensures a clean shutdown.
Adds logging for task completion and timeout scenarios.

* Refactor: Extract duplicated task waiting logic into helper method (#21)

* Initial plan

* Extract duplicated task waiting logic into WaitForTaskCompletion helper method

Co-authored-by: magicxor <8275793+magicxor@users.noreply.github.com>

* Update WinSyncScroll/ViewModels/MainViewModel.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: magicxor <8275793+magicxor@users.noreply.github.com>
Co-authored-by: Ilia Burakov <magicxor@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update WinSyncScroll/ViewModels/MainViewModel.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com>
Co-authored-by: magicxor <8275793+magicxor@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants