andrew4582

ActionExtensions

Apr 1st, 2011
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.48 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6.  
  7. namespace Core.Threading {
  8.  
  9.     /// <summary>
  10.     /// Used for .net v3.5
  11.     /// </summary>
  12.     public static class ActionExtensions {
  13.  
  14.         public static void ExecAllAndWait(this IEnumerable<Action> actions,TimeSpan timeout) {
  15.  
  16.             WaitHandle[] waitHandles = new WaitHandle[actions.Count()];
  17.  
  18.             int i = 0;
  19.  
  20.             foreach(var action in actions) {
  21.                 waitHandles[i++] = action.BeginInvoke(null,null).AsyncWaitHandle;
  22.             }
  23.  
  24.             WaitAll(waitHandles,timeout);
  25.         }
  26.  
  27.         public static List<WaitHandle> ExecAsync(this IEnumerable<Action> actions) {
  28.  
  29.             List<WaitHandle> waitHandles = new List<WaitHandle>();
  30.  
  31.             foreach(var action in actions) {
  32.                 AutoResetEvent waitHandle = new AutoResetEvent(false);
  33.                 waitHandles.Add(waitHandle);
  34.  
  35.                 ActionHandler commandExecsHandler = new ActionHandler(action,waitHandle);
  36.  
  37.                 ThreadPool.QueueUserWorkItem((state) => {
  38.  
  39.                     ((ActionHandler)state).Execute();
  40.  
  41.                 },commandExecsHandler);
  42.             }
  43.             return waitHandles;
  44.         }
  45.  
  46.         public static bool WaitAll(this List<WaitHandle> waitHandles,int timeoutMs) {
  47.             return WaitAll(waitHandles.ToArray(),timeoutMs);
  48.         }
  49.  
  50.         public static bool WaitAll(this ICollection<WaitHandle> waitHandles,int timeoutMs) {
  51.             return WaitAll(waitHandles.ToArray(),timeoutMs);
  52.         }
  53.  
  54.         public static bool WaitAll(this ICollection<WaitHandle> waitHandles,TimeSpan timeout) {
  55.             return WaitAll(waitHandles.ToArray(),(int)timeout.TotalMilliseconds);
  56.         }
  57.  
  58.         public static bool WaitAll(this List<IAsyncResult> asyncResults,TimeSpan timeout) {
  59.             var waitHandles = asyncResults.ConvertAll(x => x.AsyncWaitHandle);
  60.             return WaitAll(waitHandles.ToArray(),(int)timeout.TotalMilliseconds);
  61.         }
  62.  
  63.         public static bool WaitAll(WaitHandle[] waitHandles,TimeSpan timeout) {
  64.             return WaitAll(waitHandles,(int)timeout.TotalMilliseconds);
  65.         }
  66.  
  67.         public static bool WaitAll(WaitHandle[] waitHandles,int timeOutMs) {
  68.  
  69.             if(waitHandles == null)
  70.                 throw new ArgumentNullException("waitHandles");
  71.  
  72.             if(waitHandles.Length == 0)
  73.                 return true;
  74.  
  75.             if(Thread.CurrentThread.GetApartmentState() == ApartmentState.STA) {
  76.                 // WaitAll for multiple handles on an STA thread is not supported.
  77.                 // CurrentThread is ApartmentState.STA when run under unit tests
  78.                 var successfullyComplete = true;
  79.                 foreach(var waitHandle in waitHandles) {
  80.                     successfullyComplete = successfullyComplete
  81.                     && waitHandle.WaitOne(timeOutMs,false);
  82.                 }
  83.                 return successfullyComplete;
  84.             }
  85.  
  86.             return WaitHandle.WaitAll(waitHandles,timeOutMs,false);
  87.         }
  88.         class ActionHandler {
  89.  
  90.             public Action ActionExecute { get; private set; }
  91.             public WaitHandle Handle { get; private set; }
  92.  
  93.             public void Execute() {
  94.                 ActionExecute();
  95.             }
  96.             public ActionHandler(Action a,WaitHandle h) {
  97.                 ActionExecute = a;
  98.                 Handle = h;
  99.             }
  100.         }
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment