-
Notifications
You must be signed in to change notification settings - Fork 31
/
WinFormsSynchronizationContextAdapter.cs
39 lines (31 loc) · 1.37 KB
/
WinFormsSynchronizationContextAdapter.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
38
39
// Copyright (c) Andrew Arnott. All rights reserved.
// Licensed under the Ms-PL license. See LICENSE.txt file in the project root for full license information.
namespace Xunit.Sdk
{
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
internal class WinFormsSynchronizationContextAdapter : SyncContextAdapter
{
internal static readonly SyncContextAdapter Default = new WinFormsSynchronizationContextAdapter();
private WinFormsSynchronizationContextAdapter()
{
}
internal override bool CanCompleteOperations => false;
internal override SynchronizationContext Create() => new WindowsFormsSynchronizationContext();
internal override Task WaitForOperationCompletionAsync(SynchronizationContext syncContext)
{
throw new NotSupportedException("Async void test methods are not supported by the WinForms dispatcher. Use Async Task instead.");
}
internal override void PumpTill(SynchronizationContext synchronizationContext, Task task)
{
while (!task.IsCompleted)
{
Application.DoEvents();
Thread.Sleep(0); // give up thread to OS so we don't spin CPU too hard
}
}
internal override void InitializeThread() => Application.OleRequired();
}
}