corrade-vassal – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | vero | 1 | // Written by Peter A. Bromberg, found at |
2 | // http://www.eggheadcafe.com/articles/20060727.asp |
||
3 | |||
4 | using System; |
||
5 | using System.Threading; |
||
6 | |||
7 | /// <summary> |
||
8 | /// |
||
9 | /// </summary> |
||
10 | public class ThreadUtil |
||
11 | { |
||
12 | /// <summary> |
||
13 | /// Delegate to wrap another delegate and its arguments |
||
14 | /// </summary> |
||
15 | /// <param name="d"></param> |
||
16 | /// <param name="args"></param> |
||
17 | delegate void DelegateWrapper(Delegate d, object[] args); |
||
18 | |||
19 | /// <summary> |
||
20 | /// An instance of DelegateWrapper which calls InvokeWrappedDelegate, |
||
21 | /// which in turn calls the DynamicInvoke method of the wrapped |
||
22 | /// delegate |
||
23 | /// </summary> |
||
24 | static DelegateWrapper wrapperInstance = new DelegateWrapper(InvokeWrappedDelegate); |
||
25 | |||
26 | /// <summary> |
||
27 | /// Callback used to call EndInvoke on the asynchronously |
||
28 | /// invoked DelegateWrapper |
||
29 | /// </summary> |
||
30 | static AsyncCallback callback = new AsyncCallback(EndWrapperInvoke); |
||
31 | |||
32 | /// <summary> |
||
33 | /// Executes the specified delegate with the specified arguments |
||
34 | /// asynchronously on a thread pool thread |
||
35 | /// </summary> |
||
36 | /// <param name="d"></param> |
||
37 | /// <param name="args"></param> |
||
38 | public static void FireAndForget(Delegate d, params object[] args) |
||
39 | { |
||
40 | // Invoke the wrapper asynchronously, which will then |
||
41 | // execute the wrapped delegate synchronously (in the |
||
42 | // thread pool thread) |
||
43 | if (d != null) wrapperInstance.BeginInvoke(d, args, callback, null); |
||
44 | } |
||
45 | |||
46 | /// <summary> |
||
47 | /// Invokes the wrapped delegate synchronously |
||
48 | /// </summary> |
||
49 | /// <param name="d"></param> |
||
50 | /// <param name="args"></param> |
||
51 | static void InvokeWrappedDelegate(Delegate d, object[] args) |
||
52 | { |
||
53 | d.DynamicInvoke(args); |
||
54 | } |
||
55 | |||
56 | /// <summary> |
||
57 | /// Calls EndInvoke on the wrapper and Close on the resulting WaitHandle |
||
58 | /// to prevent resource leaks |
||
59 | /// </summary> |
||
60 | /// <param name="ar"></param> |
||
61 | static void EndWrapperInvoke(IAsyncResult ar) |
||
62 | { |
||
63 | wrapperInstance.EndInvoke(ar); |
||
64 | ar.AsyncWaitHandle.Close(); |
||
65 | } |
||
66 | } |