-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support DispatcherQueueSynchronizationContext.Send.
- Loading branch information
Showing
1 changed file
with
50 additions
and
37 deletions.
There are no files selected for viewing
87 changes: 50 additions & 37 deletions
87
src/Projections/Reunion/Microsoft.UI.Dispatching.DispatcherQueueSynchronizationContext.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 |
---|---|---|
@@ -1,38 +1,51 @@ | ||
using System; | ||
using System.Threading; | ||
using Microsoft.System; | ||
|
||
namespace Microsoft.System | ||
{ | ||
/// <summary> | ||
/// DispatcherQueueSyncContext allows developers to await calls and get back onto the | ||
/// UI thread. Needs to be installed on the UI thread through DispatcherQueueSyncContext.SetForCurrentThread | ||
/// </summary> | ||
public class DispatcherQueueSynchronizationContext : SynchronizationContext | ||
{ | ||
private readonly DispatcherQueue m_dispatcherQueue; | ||
|
||
public DispatcherQueueSynchronizationContext(DispatcherQueue dispatcherQueue) | ||
{ | ||
m_dispatcherQueue = dispatcherQueue; | ||
} | ||
|
||
public override void Post(SendOrPostCallback d, object state) | ||
{ | ||
if (d == null) | ||
throw new ArgumentNullException(nameof(d)); | ||
|
||
m_dispatcherQueue.TryEnqueue(() => d(state)); | ||
} | ||
|
||
public override void Send(SendOrPostCallback d, object state) | ||
{ | ||
throw new NotSupportedException("Send not supported"); | ||
} | ||
|
||
public override SynchronizationContext CreateCopy() | ||
{ | ||
return new DispatcherQueueSynchronizationContext(m_dispatcherQueue); | ||
} | ||
} | ||
using System; | ||
using System.Threading; | ||
using Microsoft.System; | ||
|
||
namespace Microsoft.System | ||
{ | ||
/// <summary> | ||
/// DispatcherQueueSyncContext allows developers to await calls and get back onto the | ||
/// UI thread. Needs to be installed on the UI thread through DispatcherQueueSyncContext.SetForCurrentThread | ||
/// </summary> | ||
public class DispatcherQueueSynchronizationContext : SynchronizationContext | ||
{ | ||
private readonly DispatcherQueue m_dispatcherQueue; | ||
|
||
public DispatcherQueueSynchronizationContext(DispatcherQueue dispatcherQueue) | ||
{ | ||
m_dispatcherQueue = dispatcherQueue; | ||
} | ||
|
||
public override void Post(SendOrPostCallback d, object state) | ||
{ | ||
if (d == null) | ||
throw new ArgumentNullException(nameof(d)); | ||
|
||
m_dispatcherQueue.TryEnqueue(() => d(state)); | ||
} | ||
|
||
public override void Send(SendOrPostCallback d, object state) | ||
{ | ||
if (m_dispatcherQueue.HasThreadAccess) | ||
{ | ||
d(state); | ||
} | ||
else | ||
{ | ||
var m = new ManualResetEvent(false); | ||
m_dispatcherQueue.TryEnqueue(() => | ||
{ | ||
d(state); | ||
m.Set(); | ||
}); | ||
m.WaitOne(); | ||
} | ||
} | ||
|
||
public override SynchronizationContext CreateCopy() | ||
{ | ||
return new DispatcherQueueSynchronizationContext(m_dispatcherQueue); | ||
} | ||
} | ||
} |