Debian Codegraph @

external/rx/Rx/NET/Source/Tests.System.Reactive/DispatcherHelpers.cs

raw
1 // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
2
3 #if !NO_WINDOWS_THREADING
4 using System;
5 using System.Threading;
6 using System.Windows.Threading;
7
8 namespace ReactiveTests
9 {
10 static class DispatcherHelpers
11 {
12 public static DispatcherWrapper EnsureDispatcher()
13 {
14 #if DESKTOPCLR
15 var dispatcher = new Thread(Dispatcher.Run);
16 dispatcher.IsBackground = true;
17 dispatcher.Start();
18
19 while (Dispatcher.FromThread(dispatcher) == null)
20 Thread.Sleep(10);
21
22 var d = Dispatcher.FromThread(dispatcher);
23
24 while (d.BeginInvoke(new Action(() => { })).Status == DispatcherOperationStatus.Aborted) ;
25
26 return new DispatcherWrapper(d);
27 #else
28 return new DispatcherWrapper(System.Windows.Deployment.Current.Dispatcher);
29 #endif
30 }
31 }
32
33 class DispatcherWrapper
34 {
35 private Dispatcher _dispatcher;
36
37 public DispatcherWrapper(Dispatcher dispatcher)
38 {
39 _dispatcher = dispatcher;
40 }
41
42 public Dispatcher Dispatcher { get { return _dispatcher; } }
43
44 public void InvokeShutdown()
45 {
46 #if !USE_SL_DISPATCHER
47 _dispatcher.InvokeShutdown();
48 #endif
49 }
50
51 public static implicit operator Dispatcher(DispatcherWrapper wrapper)
52 {
53 return wrapper._dispatcher;
54 }
55
56 #if !USE_SL_DISPATCHER
57 public event DispatcherUnhandledExceptionEventHandler UnhandledException
58 {
59 add { _dispatcher.UnhandledException += value; }
60 remove { _dispatcher.UnhandledException -= value; }
61 }
62 #endif
63
64 public void BeginInvoke(Action action)
65 {
66 _dispatcher.BeginInvoke(action);
67 }
68 }
69 }
70
71 #endif