forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActionExecExtensions.cs
More file actions
96 lines (82 loc) · 3.28 KB
/
ActionExecExtensions.cs
File metadata and controls
96 lines (82 loc) · 3.28 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using ServiceStack.Support;
#if NETFX_CORE
using Windows.System.Threading;
#endif
namespace ServiceStack
{
public static class ActionExecExtensions
{
public static void ExecAllAndWait(this ICollection<Action> actions, TimeSpan timeout)
{
var waitHandles = new WaitHandle[actions.Count];
var i = 0;
foreach (var action in actions)
{
waitHandles[i++] = action.BeginInvoke(null, null).AsyncWaitHandle;
}
WaitAll(waitHandles, timeout);
}
public static List<WaitHandle> ExecAsync(this IEnumerable<Action> actions)
{
var waitHandles = new List<WaitHandle>();
foreach (var action in actions)
{
var waitHandle = new AutoResetEvent(false);
waitHandles.Add(waitHandle);
var commandExecsHandler = new ActionExecHandler(action, waitHandle);
#if NETFX_CORE
ThreadPool.RunAsync(new WorkItemHandler((IAsyncAction) => commandExecsHandler.Execute()));
#else
ThreadPool.QueueUserWorkItem(x => ((ActionExecHandler)x).Execute(), commandExecsHandler);
#endif
}
return waitHandles;
}
public static bool WaitAll(this List<WaitHandle> waitHandles, int timeoutMs)
{
return WaitAll(waitHandles.ToArray(), timeoutMs);
}
public static bool WaitAll(this ICollection<WaitHandle> waitHandles, int timeoutMs)
{
return WaitAll(waitHandles.ToArray(), timeoutMs);
}
public static bool WaitAll(this ICollection<WaitHandle> waitHandles, TimeSpan timeout)
{
return WaitAll(waitHandles.ToArray(), (int)timeout.TotalMilliseconds);
}
#if !SILVERLIGHT && !MONOTOUCH && !XBOX
public static bool WaitAll(this List<IAsyncResult> asyncResults, TimeSpan timeout)
{
var waitHandles = asyncResults.ConvertAll(x => x.AsyncWaitHandle);
return WaitAll(waitHandles.ToArray(), (int)timeout.TotalMilliseconds);
}
public static bool WaitAll(WaitHandle[] waitHandles, TimeSpan timeout)
{
return WaitAll(waitHandles, (int)timeout.TotalMilliseconds);
}
public static bool WaitAll(WaitHandle[] waitHandles, int timeOutMs)
{
// throws an exception if there are no wait handles
if (waitHandles == null) throw new ArgumentNullException("waitHandles");
if (waitHandles.Length == 0) return true;
if (Thread.CurrentThread.GetApartmentState() == ApartmentState.STA)
{
// WaitAll for multiple handles on an STA thread is not supported.
// CurrentThread is ApartmentState.STA when run under unit tests
var successfullyComplete = true;
foreach (var waitHandle in waitHandles)
{
successfullyComplete = successfullyComplete
&& waitHandle.WaitOne(timeOutMs, false);
}
return successfullyComplete;
}
return WaitHandle.WaitAll(waitHandles, timeOutMs, false);
}
#endif
}
}