-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Expand file tree
/
Copy pathDispatcherThread.cs
More file actions
73 lines (57 loc) · 1.88 KB
/
Copy pathDispatcherThread.cs
File metadata and controls
73 lines (57 loc) · 1.88 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright © 2010-2015 The CefSharp Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Threading;
namespace CefSharp.Test
{
public class DispatcherThread : DisposableResource
{
public DispatcherThread()
{
startedEvent = new ManualResetEvent(false);
thread = new Thread(Run);
thread.SetApartmentState(ApartmentState.STA);
thread.Name = "DispatcherThread";
thread.Start();
startedEvent.WaitOne();
startedEvent.Dispose();
startedEvent = null;
}
private void Run()
{
frame = new DispatcherFrame(true);
dispatcher = Dispatcher.CurrentDispatcher;
Action action = () =>
{
TaskFactory = new TaskFactory(TaskScheduler.FromCurrentSynchronizationContext());
startedEvent.Set();
};
dispatcher.BeginInvoke(action);
Dispatcher.PushFrame(frame);
}
protected override void DoDispose(bool isDisposing)
{
if (frame != null)
{
frame.Continue = false;
frame = null;
}
dispatcher.BeginInvokeShutdown( DispatcherPriority.Normal );
if (!thread.Join(TimeSpan.FromSeconds(5)))
{
thread.Abort();
thread.Interrupt();
}
thread = null;
base.DoDispose(isDisposing);
}
private DispatcherFrame frame;
private ManualResetEvent startedEvent;
private Thread thread;
private Dispatcher dispatcher;
public TaskFactory TaskFactory { get; private set; }
}
}