private static bool WaitMany(waitableasync[] arrWAsync, int milliSeconds, bool waitAll) { bool result = false; List <WaitHandle> handleList = new List <WaitHandle>(); foreach (waitableasync wasync in arrWAsync) { WaitableAsyncDelegateTask wadt = wasync._task as WaitableAsyncDelegateTask; if (wadt != null) { WaitHandle wh = wadt.TaskWaitHandle; if (handleList.Contains(wh) == false) { handleList.Add(wh); } } } WaitHandle[] whList = handleList.ToArray(); if (waitAll == true) { result = WaitHandle.WaitAll(whList, milliSeconds, false); } else { result = (WaitHandle.WaitAny(whList, milliSeconds, false) != WaitHandle.WaitTimeout) ? true : false; } return(result); }
/// <summary> /// Calling code will wait until the code wrapped by this instance /// of the waitableasync object is executed or the specified time /// is elapsed. /// </summary> /// <param name="msWaitTime"> /// Time in milliseconds to wait for the completion of /// code execution /// </param> /// <returns> /// true, if the wait returned after the completion of execution /// of the wrapped code. false, if the wait returned due to time /// out before completion of execution of code /// </returns> public override bool Wait(int msWaitTime) { bool waitDone = false; WaitableAsyncDelegateTask wadt = _task as WaitableAsyncDelegateTask; if (wadt != null) { waitDone = wadt.Wait(msWaitTime, WaitableGenericTask.WaitType.ReuseWaitEvent); } return(waitDone); }
protected void Initialize(AsyncDelegate ad, Sonic.Net.ThreadPool tp, Async[] arrDependentOnAsync, AsyncCodeBlockExecutionCompleteCallback executionCompleteCallback, bool waitable) { if (ad != null) { _waitable = waitable; _ad = ad; _tp = tp; _targetObject = ad.Target; _targetType = ad.Method.DeclaringType; if (_waitable == false) { AsyncDelegateTask adt = new AsyncDelegateTask(ad); adt.TaskCompleted = this.MarkCompleted; _task = adt; } else { WaitableAsyncDelegateTask wadt = new WaitableAsyncDelegateTask(ad); wadt.TaskCompleted = this.MarkCompleted; _task = wadt; } _executionCompleteCallback = executionCompleteCallback; bool dispatchForExecution = true; if (arrDependentOnAsync != null) { lock (_syncObject) { foreach (Async asyncObj in arrDependentOnAsync) { if (asyncObj.AddToDependencyCodeBlockList(this) == true) { _dependentCount++; } } if (_dependentCount == 0) { dispatchForExecution = true; } else { dispatchForExecution = false; } } } // Store the current SynchronizationContext // _synchronizationContext = SynchronizationContext.Current; if (dispatchForExecution == true) { Execute(ad, _task, tp); } } }
private static bool WaitOne(waitableasync wasync, int milliSeconds) { bool result = false; WaitableAsyncDelegateTask wadt = wasync._task as WaitableAsyncDelegateTask; if (wadt != null) { WaitHandle wh = wadt.TaskWaitHandle; result = wh.WaitOne(milliSeconds, false); } return(result); }