| 1 |
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. |
| 2 |
|
| 3 |
#if NO_THREAD && WINDOWS |
| 4 |
using System; |
| 5 |
using System.Collections.Generic; |
| 6 |
using System.Reactive.Disposables; |
| 7 |
using System.Threading; |
| 8 |
|
| 9 |
namespace System.Reactive.Concurrency |
| 10 |
{ |
| 11 |
internal class /*Default*/ConcurrencyAbstractionLayerImpl : IConcurrencyAbstractionLayer |
| 12 |
{ |
| 13 |
public IDisposable StartTimer(Action<object> action, object state, TimeSpan dueTime) |
| 14 |
{ |
| 15 |
var res = global::Windows.System.Threading.ThreadPoolTimer.CreateTimer( |
| 16 |
tpt => |
| 17 |
{ |
| 18 |
action(state); |
| 19 |
}, |
| 20 |
Normalize(dueTime) |
| 21 |
); |
| 22 |
|
| 23 |
return Disposable.Create(res.Cancel); |
| 24 |
} |
| 25 |
|
| 26 |
public IDisposable StartPeriodicTimer(Action action, TimeSpan period) |
| 27 |
{ |
| 28 |
// |
| 29 |
// The WinRT thread pool is based on the Win32 thread pool and cannot handle |
| 30 |
// sub-1ms resolution. When passing a lower period, we get single-shot |
| 31 |
// timer behavior instead. See MSDN documentation for CreatePeriodicTimer |
| 32 |
// for more information. |
| 33 |
// |
| 34 |
if (period < TimeSpan.FromMilliseconds(1)) |
| 35 |
throw new ArgumentOutOfRangeException("period", Strings_PlatformServices.WINRT_NO_SUB1MS_TIMERS); |
| 36 |
|
| 37 |
var res = global::Windows.System.Threading.ThreadPoolTimer.CreatePeriodicTimer( |
| 38 |
tpt => |
| 39 |
{ |
| 40 |
action(); |
| 41 |
}, |
| 42 |
period |
| 43 |
); |
| 44 |
|
| 45 |
return Disposable.Create(res.Cancel); |
| 46 |
} |
| 47 |
|
| 48 |
public IDisposable QueueUserWorkItem(Action<object> action, object state) |
| 49 |
{ |
| 50 |
var res = global::Windows.System.Threading.ThreadPool.RunAsync(iaa => |
| 51 |
{ |
| 52 |
action(state); |
| 53 |
}); |
| 54 |
|
| 55 |
return Disposable.Create(res.Cancel); |
| 56 |
} |
| 57 |
|
| 58 |
public void Sleep(TimeSpan timeout) |
| 59 |
{ |
| 60 |
var e = new ManualResetEventSlim(); |
| 61 |
|
| 62 |
global::Windows.System.Threading.ThreadPoolTimer.CreateTimer( |
| 63 |
tpt => |
| 64 |
{ |
| 65 |
e.Set(); |
| 66 |
}, |
| 67 |
Normalize(timeout) |
| 68 |
); |
| 69 |
|
| 70 |
e.Wait(); |
| 71 |
} |
| 72 |
|
| 73 |
public IStopwatch StartStopwatch() |
| 74 |
{ |
| 75 |
#if !NO_STOPWATCH |
| 76 |
return new StopwatchImpl(); |
| 77 |
#else |
| 78 |
return new DefaultStopwatch(); |
| 79 |
#endif |
| 80 |
} |
| 81 |
|
| 82 |
public bool SupportsLongRunning |
| 83 |
{ |
| 84 |
get { return false; } |
| 85 |
} |
| 86 |
|
| 87 |
public void StartThread(Action<object> action, object state) |
| 88 |
{ |
| 89 |
throw new NotSupportedException(); |
| 90 |
} |
| 91 |
|
| 92 |
private TimeSpan Normalize(TimeSpan dueTime) |
| 93 |
{ |
| 94 |
if (dueTime < TimeSpan.Zero) |
| 95 |
return TimeSpan.Zero; |
| 96 |
|
| 97 |
return dueTime; |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
#endif |