forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandsUtils.cs
More file actions
119 lines (108 loc) · 3.98 KB
/
CommandsUtils.cs
File metadata and controls
119 lines (108 loc) · 3.98 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using System;
using System.Collections.Generic;
using System.Threading;
using ServiceStack.Commands;
using ServiceStack.Support;
#if NETFX_CORE
using Windows.System.Threading;
#endif
namespace ServiceStack
{
public class CommandsUtils
{
public static List<T> ExecuteAsyncCommandList<T>(TimeSpan timeout, params ICommandList<T>[] commands)
{
return ExecuteAsyncCommandList(timeout, commands);
}
public static List<T> ExecuteAsyncCommandList<T>(TimeSpan timeout, IEnumerable<ICommandList<T>> commands)
{
var results = new List<T>();
var waitHandles = new List<WaitHandle>();
foreach (ICommandList<T> command in commands)
{
var waitHandle = new AutoResetEvent(false);
waitHandles.Add(waitHandle);
var commandResultsHandler = new CommandResultsHandler<T>(results, command, waitHandle);
#if NETFX_CORE
ThreadPool.RunAsync(new WorkItemHandler((IAsyncAction) => ExecuteCommandList(commandResultsHandler)));
#else
ThreadPool.QueueUserWorkItem(ExecuteCommandList, commandResultsHandler);
#endif
}
WaitAll(waitHandles.ToArray(), timeout);
return results;
}
public static void WaitAll(WaitHandle[] waitHandles, TimeSpan timeout)
{
// throws an exception if there are no wait handles
if (waitHandles != null && waitHandles.Length > 0)
{
#if !SL5 && !IOS && !XBOX
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
foreach (WaitHandle waitHandle in waitHandles)
{
waitHandle.WaitOne(timeout, false);
}
}
else
{
if (!WaitHandle.WaitAll(waitHandles, timeout, false))
{
throw new TimeoutException();
}
}
#else
if (!WaitHandle.WaitAll(waitHandles, timeout))
{
throw new TimeoutException();
}
#endif
}
}
private static void ExecuteCommandList(object state)
{
var handler = (ICommandExec)state;
handler.Execute();
}
private static void ExecuteCommandExec(object state)
{
var command = (ICommandExec)state;
command.Execute();
}
public static void ExecuteAsyncCommandExec(TimeSpan timeout, IEnumerable<ICommandExec> commands)
{
foreach (ICommandExec command in commands)
{
#if NETFX_CORE
ThreadPool.RunAsync(new WorkItemHandler((IAsyncAction) => ExecuteCommandExec(command)));
#else
ThreadPool.QueueUserWorkItem(ExecuteCommandExec, command);
#endif
}
}
/// <summary>
/// Provide the an option for the callee to block until all commands are executed
/// </summary>
/// <param name="commands"></param>
/// <returns></returns>
public static List<WaitHandle> ExecuteAsyncCommandExec(IEnumerable<ICommandExec> commands)
{
var waitHandles = new List<WaitHandle>();
foreach (var command in commands)
{
var waitHandle = new AutoResetEvent(false);
waitHandles.Add(waitHandle);
var commandExecsHandler = new CommandExecsHandler(command, waitHandle);
#if NETFX_CORE
ThreadPool.RunAsync(new WorkItemHandler((IAsyncAction) => ExecuteCommandList(commandExecsHandler)));
#else
ThreadPool.QueueUserWorkItem(ExecuteCommandList, commandExecsHandler);
#endif
}
return waitHandles;
}
}
}