Skip to content

Provide visibility to running synchronously#16

Merged
centeredgebot[bot] merged 2 commits into
mainfrom
runsync-visibility
Oct 24, 2025
Merged

Provide visibility to running synchronously#16
centeredgebot[bot] merged 2 commits into
mainfrom
runsync-visibility

Conversation

@brantburnett
Copy link
Copy Markdown
Contributor

Motivation

There are cases in complex systems where it is helpful for code to know that it is running within a call to AsyncHelper.RunSync. Furthermore, retrieving the previously replaced synchronization context can also be helpful. For example, a call to Form.ShowDialog in a WinForms app may want to reinstall the WindowsFormsSynchronizationContext since a new message pump will be running to process continuations.

Modifications

  • Add IsRunningSynchronously property
  • Add GetReplacedSynchronizationContext method
  • Track the fact we're in the call to RunSync and processing work on its calling thread using a thread static

Motivation
----------
There are cases in complex systems where it is helpful for code to know
that it is running within a call to AsyncHelper.RunSync. Furthermore,
retrieving the previously replaced synchronization context can also be
helpful. For example, a call to Form.ShowDialog in a WinForms app may
want to reinstall the WindowsFormsSynchronizationContext since a new
message pump will be running to process continuations.

Modifications
-------------
- Add IsRunningSynchronously property
- Add GetReplacedSynchronizationContext method
- Track the fact we're in the call to RunSync and processing work on its
  calling thread using a thread static
@brantburnett brantburnett requested a review from Copilot October 24, 2025 14:09
Copy link
Copy Markdown

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 introduces visibility into synchronous execution context by exposing whether code is running within AsyncHelper.RunSync and providing access to the replaced synchronization context. This enables scenarios like WinForms applications to reinstall their synchronization context when opening dialogs within async code.

Key Changes:

  • Added IsRunningSynchronously property to detect execution within RunSync
  • Added GetReplacedSynchronizationContext() method to retrieve the outermost replaced synchronization context
  • Implemented thread-static tracking to monitor RunSync execution state

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
ExclusiveSynchronizationContext.cs Exposes parent synchronization context via public property
AsyncHelper.cs Adds synchronous execution tracking with thread-static flag and new public API methods
CenterEdge.Async.UnitTests.csproj Enables nullable reference types for stronger type safety
AsyncHelperTests.cs Adds comprehensive test coverage for new synchronization context visibility features

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment thread src/CenterEdge.Async/AsyncHelper.cs Outdated
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Signed-off-by: Brant Burnett <bburnett@centeredgesoftware.com>
@brantburnett brantburnett marked this pull request as ready for review October 24, 2025 14:10
@brantburnett
Copy link
Copy Markdown
Contributor Author

/merge

@centeredgebot centeredgebot Bot merged commit 332638a into main Oct 24, 2025
6 checks passed
@centeredgebot centeredgebot Bot deleted the runsync-visibility branch October 24, 2025 14:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants