-
Notifications
You must be signed in to change notification settings - Fork 4
/
UiThreadSynchronizationContext.cs
37 lines (30 loc) · 1.28 KB
/
UiThreadSynchronizationContext.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using System.Collections.Concurrent;
using Windows.Win32;
using Windows.Win32.Foundation;
namespace MinimalWebView;
// based on this very good Stephen Toub article: https://devblogs.microsoft.com/pfxteam/await-synchronizationcontext-and-console-apps/
internal sealed class UiThreadSynchronizationContext : SynchronizationContext
{
private readonly BlockingCollection<KeyValuePair<SendOrPostCallback, object>> m_queue = new();
private readonly HWND hwnd;
public UiThreadSynchronizationContext(HWND hwnd) : base()
{
this.hwnd = hwnd;
}
public override void Post(SendOrPostCallback d, object state)
{
m_queue.Add(new KeyValuePair<SendOrPostCallback, object>(d, state));
PInvoke.PostMessage(hwnd, Program.WM_SYNCHRONIZATIONCONTEXT_WORK_AVAILABLE, 0, 0);
}
public override void Send(SendOrPostCallback d, object state)
{
m_queue.Add(new KeyValuePair<SendOrPostCallback, object>(d, state));
PInvoke.SendMessage(hwnd, Program.WM_SYNCHRONIZATIONCONTEXT_WORK_AVAILABLE, 0, 0);
}
public void RunAvailableWorkOnCurrentThread()
{
while (m_queue.TryTake(out KeyValuePair<SendOrPostCallback, object> workItem))
workItem.Key(workItem.Value);
}
public void Complete() { m_queue.CompleteAdding(); }
}