-
Notifications
You must be signed in to change notification settings - Fork 437
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Old example is still there for reference, just not used by default - Add AsyncContext/SingleThreadSynchronizationContext to ensure async calls continue on main thread.
- Loading branch information
Showing
4 changed files
with
247 additions
and
54 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/// https://devblogs.microsoft.com/pfxteam/await-synchronizationcontext-and-console-apps/ | ||
|
||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace CefSharp.MinimalExample.OffScreen | ||
{ | ||
public static class AsyncContext | ||
{ | ||
public static void Run(Func<Task> func) | ||
{ | ||
var prevCtx = SynchronizationContext.Current; | ||
|
||
try | ||
{ | ||
var syncCtx = new SingleThreadSynchronizationContext(); | ||
|
||
SynchronizationContext.SetSynchronizationContext(syncCtx); | ||
|
||
var t = func(); | ||
|
||
t.ContinueWith(delegate | ||
{ | ||
syncCtx.Complete(); | ||
}, TaskScheduler.Default); | ||
|
||
syncCtx.RunOnCurrentThread(); | ||
|
||
t.GetAwaiter().GetResult(); | ||
} | ||
finally | ||
{ | ||
SynchronizationContext.SetSynchronizationContext(prevCtx); | ||
} | ||
} | ||
} | ||
} |
This file contains 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 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
32 changes: 32 additions & 0 deletions
32
CefSharp.MinimalExample.OffScreen/SingleThreadSynchronizationContext.cs
This file contains 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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/// https://devblogs.microsoft.com/pfxteam/await-synchronizationcontext-and-console-apps/ | ||
|
||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
|
||
namespace CefSharp.MinimalExample.OffScreen | ||
{ | ||
public sealed class SingleThreadSynchronizationContext : SynchronizationContext | ||
{ | ||
private readonly BlockingCollection<KeyValuePair<SendOrPostCallback, object>> queue = | ||
new BlockingCollection<KeyValuePair<SendOrPostCallback, object>>(); | ||
|
||
public override void Post(SendOrPostCallback d, object state) | ||
{ | ||
queue.Add(new KeyValuePair<SendOrPostCallback, object>(d, state)); | ||
} | ||
|
||
public void RunOnCurrentThread() | ||
{ | ||
while (queue.TryTake(out var workItem, Timeout.Infinite)) | ||
{ | ||
workItem.Key(workItem.Value); | ||
} | ||
} | ||
|
||
public void Complete() | ||
{ | ||
queue.CompleteAdding(); | ||
} | ||
} | ||
} |