// Ami Bar // amibar@gmail.com using System; using System.Threading; namespace Amib.Threading { #region Delegates /// /// A delegate that represents the method to run as the work item /// /// A state object for the method to run public delegate object WorkItemCallback(object state); /// /// A delegate to call after the WorkItemCallback completed /// /// The work item result object public delegate void PostExecuteWorkItemCallback(IWorkItemResult wir); /// /// A delegate to call when a WorkItemsGroup becomes idle /// /// A reference to the WorkItemsGroup that became idle public delegate void WorkItemsGroupIdleHandler(IWorkItemsGroup workItemsGroup); #endregion #region WorkItem Priority public enum WorkItemPriority { Lowest, BelowNormal, Normal, AboveNormal, Highest, } #endregion #region IHasWorkItemPriority interface public interface IHasWorkItemPriority { WorkItemPriority WorkItemPriority { get; } } #endregion #region IWorkItemsGroup interface /// /// IWorkItemsGroup interface /// public interface IWorkItemsGroup { /// /// Get/Set the name of the WorkItemsGroup /// string Name { get; set; } IWorkItemResult QueueWorkItem(WorkItemCallback callback); IWorkItemResult QueueWorkItem(WorkItemCallback callback, WorkItemPriority workItemPriority); IWorkItemResult QueueWorkItem(WorkItemCallback callback, object state); IWorkItemResult QueueWorkItem(WorkItemCallback callback, object state, WorkItemPriority workItemPriority); IWorkItemResult QueueWorkItem(WorkItemCallback callback, object state, PostExecuteWorkItemCallback postExecuteWorkItemCallback); IWorkItemResult QueueWorkItem(WorkItemCallback callback, object state, PostExecuteWorkItemCallback postExecuteWorkItemCallback, WorkItemPriority workItemPriority); IWorkItemResult QueueWorkItem(WorkItemCallback callback, object state, PostExecuteWorkItemCallback postExecuteWorkItemCallback, CallToPostExecute callToPostExecute); IWorkItemResult QueueWorkItem(WorkItemCallback callback, object state, PostExecuteWorkItemCallback postExecuteWorkItemCallback, CallToPostExecute callToPostExecute, WorkItemPriority workItemPriority); IWorkItemResult QueueWorkItem(WorkItemInfo workItemInfo, WorkItemCallback callback); IWorkItemResult QueueWorkItem(WorkItemInfo workItemInfo, WorkItemCallback callback, object state); void WaitForIdle(); bool WaitForIdle(TimeSpan timeout); bool WaitForIdle(int millisecondsTimeout); int WaitingCallbacks { get; } event WorkItemsGroupIdleHandler OnIdle; void Cancel(); void Start(); } #endregion #region CallToPostExecute enumerator [Flags] public enum CallToPostExecute { Never = 0x00, WhenWorkItemCanceled = 0x01, WhenWorkItemNotCanceled = 0x02, Always = WhenWorkItemCanceled | WhenWorkItemNotCanceled, } #endregion #region IWorkItemResult interface /// /// IWorkItemResult interface /// public interface IWorkItemResult { /// /// Get the result of the work item. /// If the work item didn't run yet then the caller waits. /// /// The result of the work item object GetResult(); /// /// Get the result of the work item. /// If the work item didn't run yet then the caller waits until timeout. /// /// The result of the work item /// On timeout throws WorkItemTimeoutException object GetResult( int millisecondsTimeout, bool exitContext); /// /// Get the result of the work item. /// If the work item didn't run yet then the caller waits until timeout. /// /// The result of the work item /// On timeout throws WorkItemTimeoutException object GetResult( TimeSpan timeout, bool exitContext); void Abort(); /// /// Get the result of the work item. /// If the work item didn't run yet then the caller waits until timeout or until the cancelWaitHandle is signaled. /// /// Timeout in milliseconds, or -1 for infinite /// /// true to exit the synchronization domain for the context before the wait (if in a synchronized context), and reacquire it; otherwise, false. /// /// A cancel wait handle to interrupt the blocking if needed /// The result of the work item /// On timeout throws WorkItemTimeoutException /// On cancel throws WorkItemCancelException object GetResult( int millisecondsTimeout, bool exitContext, WaitHandle cancelWaitHandle); /// /// Get the result of the work item. /// If the work item didn't run yet then the caller waits until timeout or until the cancelWaitHandle is signaled. /// /// The result of the work item /// On timeout throws WorkItemTimeoutException /// On cancel throws WorkItemCancelException object GetResult( TimeSpan timeout, bool exitContext, WaitHandle cancelWaitHandle); /// /// Get the result of the work item. /// If the work item didn't run yet then the caller waits. /// /// Filled with the exception if one was thrown /// The result of the work item object GetResult(out Exception e); /// /// Get the result of the work item. /// If the work item didn't run yet then the caller waits until timeout. /// /// Filled with the exception if one was thrown /// The result of the work item /// On timeout throws WorkItemTimeoutException object GetResult( int millisecondsTimeout, bool exitContext, out Exception e); /// /// Get the result of the work item. /// If the work item didn't run yet then the caller waits until timeout. /// /// Filled with the exception if one was thrown /// The result of the work item /// On timeout throws WorkItemTimeoutException object GetResult( TimeSpan timeout, bool exitContext, out Exception e); /// /// Get the result of the work item. /// If the work item didn't run yet then the caller waits until timeout or until the cancelWaitHandle is signaled. /// /// Timeout in milliseconds, or -1 for infinite /// /// true to exit the synchronization domain for the context before the wait (if in a synchronized context), and reacquire it; otherwise, false. /// /// A cancel wait handle to interrupt the blocking if needed /// Filled with the exception if one was thrown /// The result of the work item /// On timeout throws WorkItemTimeoutException /// On cancel throws WorkItemCancelException object GetResult( int millisecondsTimeout, bool exitContext, WaitHandle cancelWaitHandle, out Exception e); /// /// Get the result of the work item. /// If the work item didn't run yet then the caller waits until timeout or until the cancelWaitHandle is signaled. /// /// The result of the work item /// Filled with the exception if one was thrown /// On timeout throws WorkItemTimeoutException /// On cancel throws WorkItemCancelException object GetResult( TimeSpan timeout, bool exitContext, WaitHandle cancelWaitHandle, out Exception e); /// /// Gets an indication whether the asynchronous operation has completed. /// bool IsCompleted { get; } /// /// Gets an indication whether the asynchronous operation has been canceled. /// bool IsCanceled { get; } /// /// Gets a user-defined object that qualifies or contains information about an asynchronous operation. /// object State { get; } /// /// Cancel the work item if it didn't start running yet. /// /// Returns true on success or false if the work item is in progress or already completed bool Cancel(); /// /// Get the work item's priority /// WorkItemPriority WorkItemPriority { get; } /// /// Return the result, same as GetResult() /// object Result { get; } /// /// Returns the exception if occured otherwise returns null. /// object Exception { get; } } #endregion }