-
Notifications
You must be signed in to change notification settings - Fork 781
Fixes #5579. SynchronizationContext is not correctly implemented in v2 #5588
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+202
−44
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
50f96c8
Fixes #5579. SynchronizationContext is not correctly implemented in v2
BDisp d60a665
Enforces that apps can be crested before call Run and the Begin metho…
BDisp 007ba80
Potential fix for pull request finding
BDisp 19aa5b0
Fix nullable warning
BDisp 309c393
Reset callbackCalled before reusing it
BDisp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,43 +1,78 @@ | ||
| #nullable disable | ||
| namespace Terminal.Gui.App; | ||
|
|
||
| ///// <summary> | ||
| ///// provides the sync context set while executing code in Terminal.Gui, to let | ||
| ///// users use async/await on their code | ||
| ///// </summary> | ||
| //internal sealed class MainLoopSyncContext : SynchronizationContext | ||
| //{ | ||
| // public override SynchronizationContext CreateCopy () { return new MainLoopSyncContext (); } | ||
|
|
||
| // public override void Post (SendOrPostCallback d, object state) | ||
| // { | ||
| // // Queue the task using the modern architecture | ||
| // ApplicationImpl.Instance.Invoke (() => { d (state); }); | ||
| // } | ||
|
|
||
| // //_mainLoop.Driver.Wakeup (); | ||
| // public override void Send (SendOrPostCallback d, object state) | ||
| // { | ||
| // if (Thread.CurrentThread.ManagedThreadId == Application.MainThreadId) | ||
| // { | ||
| // d (state); | ||
| // } | ||
| // else | ||
| // { | ||
| // var wasExecuted = false; | ||
|
|
||
| // ApplicationImpl.Instance.Invoke ( | ||
| // () => | ||
| // { | ||
| // d (state); | ||
| // wasExecuted = true; | ||
| // } | ||
| // ); | ||
|
|
||
| // while (!wasExecuted) | ||
| // { | ||
| // Thread.Sleep (15); | ||
| // } | ||
| // } | ||
| // } | ||
| //} | ||
| /// <summary> | ||
| /// Provides the sync context set while executing code in Terminal.Gui, to let | ||
| /// users use async/await on their code | ||
| /// </summary> | ||
| internal sealed class MainLoopSyncContext : SynchronizationContext | ||
| { | ||
| private readonly IApplication _app; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="MainLoopSyncContext"/> class. | ||
| /// </summary> | ||
| /// <param name="app">The application instance that owns the main loop.</param> | ||
| public MainLoopSyncContext (IApplication app) => _app = app; | ||
|
|
||
| /// <inheritdoc/> | ||
| public override SynchronizationContext CreateCopy () => new MainLoopSyncContext (_app); | ||
|
|
||
| /// <inheritdoc/> | ||
| public override void Post (SendOrPostCallback d, object? state) | ||
| { | ||
| ArgumentNullException.ThrowIfNull (d); | ||
|
|
||
| // Queue the task using the modern architecture | ||
| _app.Invoke (() => d (state)); | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public override void Send (SendOrPostCallback d, object? state) | ||
| { | ||
| ArgumentNullException.ThrowIfNull (d); | ||
|
|
||
| if (_app.MainThreadId == Thread.CurrentThread.ManagedThreadId) | ||
| { | ||
| d (state); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| object gate = new (); | ||
| bool wasExecuted = false; | ||
| Exception? error = null; | ||
|
|
||
| _app.Invoke (() => | ||
| { | ||
| try | ||
| { | ||
| d (state); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| error = ex; | ||
| } | ||
| finally | ||
| { | ||
| lock (gate) | ||
| { | ||
| wasExecuted = true; | ||
| Monitor.Pulse (gate); | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| lock (gate) | ||
| { | ||
| while (!wasExecuted) | ||
| { | ||
| Monitor.Wait (gate); | ||
| } | ||
| } | ||
|
|
||
| if (error is { }) | ||
| { | ||
| throw error; | ||
| } | ||
|
BDisp marked this conversation as resolved.
|
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.