forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadPoolSyncContext.cs
More file actions
34 lines (31 loc) · 1.39 KB
/
Copy pathThreadPoolSyncContext.cs
File metadata and controls
34 lines (31 loc) · 1.39 KB
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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Threading;
namespace Microsoft.TestCommon
{
/// <summary>
/// This is an implementation of SynchronizationContext that not only queues things on the thread pool for
/// later work, but also ensures that it sets itself back as the synchronization context (something that the
/// default implementatation of SynchronizationContext does not do).
/// </summary>
public class ThreadPoolSyncContext : SynchronizationContext
{
public override void Post(SendOrPostCallback d, object state)
{
ThreadPool.QueueUserWorkItem(_ =>
{
SynchronizationContext oldContext = SynchronizationContext.Current;
SynchronizationContext.SetSynchronizationContext(this);
d.Invoke(state);
SynchronizationContext.SetSynchronizationContext(oldContext);
}, null);
}
public override void Send(SendOrPostCallback d, object state)
{
SynchronizationContext oldContext = SynchronizationContext.Current;
SynchronizationContext.SetSynchronizationContext(this);
d.Invoke(state);
SynchronizationContext.SetSynchronizationContext(oldContext);
}
}
}