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 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); /// /// A delegate to call after a thread is created, but before /// it's first use. /// public delegate void ThreadInitializationHandler(); /// /// A delegate to call when a thread is about to exit, after /// it is no longer belong to the pool. /// public delegate void ThreadTerminationHandler(); #endregion #region IWorkItemsGroup interface /// /// IWorkItemsGroup interface /// Created by SmartThreadPool.CreateWorkItemsGroup() /// public interface IWorkItemsGroup { /// /// Get/Set the name of the WorkItemsGroup /// string Name { get; set; } int localID { get; set; } /// /// Get/Set the maximum number of workitem that execute cocurrency on the thread pool /// int Concurrency { get; set; } /// /// Get the number of work items waiting in the queue. /// int WaitingCallbacks { get; } /// /// Get an array with all the state objects of the currently running items. /// The array represents a snap shot and impact performance. /// object[] GetStates(); /// /// Get the WorkItemsGroup start information /// WIGStartInfo WIGStartInfo { get; } /// /// Starts to execute work items /// void Start(); /// /// Cancel all the work items. /// Same as Cancel(false) /// void Cancel(); /// /// Cancel all work items using thread abortion /// /// True to stop work items by raising ThreadAbortException void Cancel(bool abortExecution); /// /// Wait for all work item to complete. /// void WaitForIdle(); /// /// Wait for all work item to complete, until timeout expired /// /// How long to wait for the work items to complete /// Returns true if work items completed within the timeout, otherwise false. bool WaitForIdle(TimeSpan timeout); /// /// Wait for all work item to complete, until timeout expired /// /// How long to wait for the work items to complete in milliseconds /// Returns true if work items completed within the timeout, otherwise false. bool WaitForIdle(int millisecondsTimeout); /// /// IsIdle is true when there are no work items running or queued. /// bool IsIdle { get; } /// /// This event is fired when all work items are completed. /// (When IsIdle changes to true) /// This event only work on WorkItemsGroup. On SmartThreadPool /// it throws the NotImplementedException. /// event WorkItemsGroupIdleHandler OnIdle; #region QueueWorkItem IWorkItemResult QueueWorkItem(WaitCallback callback); IWorkItemResult QueueWorkItem(WaitCallback callback, object state); IWorkItemResult QueueWorkItem(WorkItemInfo workItemInfo, WaitCallback callback); IWorkItemResult QueueWorkItem(WorkItemInfo workItemInfo, WaitCallback callback, object state); /// /// Queue a work item /// /// A callback to execute /// Returns a work item result IWorkItemResult QueueWorkItem(WorkItemCallback callback); /// /// Queue a work item /// /// A callback to execute /// /// The context object of the work item. Used for passing arguments to the work item. /// /// Returns a work item result IWorkItemResult QueueWorkItem(WorkItemCallback callback, object state); /// /// Queue a work item /// /// A callback to execute /// /// The context object of the work item. Used for passing arguments to the work item. /// /// /// A delegate to call after the callback completion /// /// Returns a work item result IWorkItemResult QueueWorkItem(WorkItemCallback callback, object state, PostExecuteWorkItemCallback postExecuteWorkItemCallback); /// /// Queue a work item /// /// A callback to execute /// /// The context object of the work item. Used for passing arguments to the work item. /// /// /// A delegate to call after the callback completion /// /// Indicates on which cases to call to the post execute callback /// Returns a work item result IWorkItemResult QueueWorkItem(WorkItemCallback callback, object state, PostExecuteWorkItemCallback postExecuteWorkItemCallback, CallToPostExecute callToPostExecute); /// /// Queue a work item /// /// Work item info /// A callback to execute /// Returns a work item result IWorkItemResult QueueWorkItem(WorkItemInfo workItemInfo, WorkItemCallback callback); /// /// Queue a work item /// /// Work item information /// A callback to execute /// /// The context object of the work item. Used for passing arguments to the work item. /// /// Returns a work item result IWorkItemResult QueueWorkItem(WorkItemInfo workItemInfo, WorkItemCallback callback, object state); #endregion #region QueueWorkItem(Action<...>) /// /// Queue a work item. /// /// Returns a IWorkItemResult object, but its GetResult() will always return null IWorkItemResult QueueWorkItem(Action action); /// /// Queue a work item. /// /// Returns a IWorkItemResult object, but its GetResult() will always return null IWorkItemResult QueueWorkItem(Action action, T arg); /// /// Queue a work item. /// /// Returns a IWorkItemResult object, but its GetResult() will always return null IWorkItemResult QueueWorkItem(Action action, T1 arg1, T2 arg2); /// /// Queue a work item. /// /// Returns a IWorkItemResult object, but its GetResult() will always return null IWorkItemResult QueueWorkItem(Action action, T1 arg1, T2 arg2, T3 arg3); /// /// Queue a work item. /// /// Returns a IWorkItemResult object, but its GetResult() will always return null IWorkItemResult QueueWorkItem(Action action, T1 arg1, T2 arg2, T3 arg3, T4 arg4); #endregion #region QueueWorkItem(Func<...>) /// /// Queue a work item. /// /// Returns a IWorkItemResult<TResult> object. /// its GetResult() returns a TResult object IWorkItemResult QueueWorkItem(Func func); /// /// Queue a work item. /// /// Returns a IWorkItemResult<TResult> object. /// its GetResult() returns a TResult object IWorkItemResult QueueWorkItem(Func func, T arg); /// /// Queue a work item. /// /// Returns a IWorkItemResult<TResult> object. /// its GetResult() returns a TResult object IWorkItemResult QueueWorkItem(Func func, T1 arg1, T2 arg2); /// /// Queue a work item. /// /// Returns a IWorkItemResult<TResult> object. /// its GetResult() returns a TResult object IWorkItemResult QueueWorkItem(Func func, T1 arg1, T2 arg2, T3 arg3); /// /// Queue a work item. /// /// Returns a IWorkItemResult<TResult> object. /// its GetResult() returns a TResult object IWorkItemResult QueueWorkItem(Func func, T1 arg1, T2 arg2, T3 arg3, T4 arg4); #endregion } #endregion #region CallToPostExecute enumerator [Flags] public enum CallToPostExecute { /// /// Never call to the PostExecute call back /// Never = 0x00, /// /// Call to the PostExecute only when the work item is cancelled /// WhenWorkItemCanceled = 0x01, /// /// Call to the PostExecute only when the work item is not cancelled /// WhenWorkItemNotCanceled = 0x02, /// /// Always call to the PostExecute /// Always = WhenWorkItemCanceled | WhenWorkItemNotCanceled, } #endregion #region IWorkItemResult interface /// /// The common interface of IWorkItemResult and IWorkItemResult<T> /// public interface IWaitableResult { /// /// This method intent is for internal use. /// /// IWorkItemResult GetWorkItemResult(); /// /// This method intent is for internal use. /// /// IWorkItemResult GetWorkItemResultT(); } /// /// IWorkItemResult interface. /// Created when a WorkItemCallback work item is queued. /// public interface IWorkItemResult : IWorkItemResult { } /// /// IWorkItemResult<TResult> interface. /// Created when a Func<TResult> work item is queued. /// public interface IWorkItemResult : IWaitableResult { /// /// 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 TResult 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 TResult 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 TResult GetResult( TimeSpan timeout, bool exitContext); /// /// 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 TResult 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 TResult 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 TResult 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 TResult 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 TResult 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 TResult 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 TResult 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 the user-defined object that contains context data /// for the work item method. /// object State { get; } /// /// Same as Cancel(false). /// bool Cancel(); /// /// Cancel the work item execution. /// If the work item is in the queue then it won't execute /// If the work item is completed, it will remain completed /// If the work item is in progress then the user can check the SmartThreadPool.IsWorkItemCanceled /// property to check if the work item has been cancelled. If the abortExecution is set to true then /// the Smart Thread Pool will send an AbortException to the running thread to stop the execution /// of the work item. When an in progress work item is canceled its GetResult will throw WorkItemCancelException. /// If the work item is already cancelled it will remain cancelled /// /// When true send an AbortException to the executing thread. /// Returns true if the work item was not completed, otherwise false. bool Cancel(bool abortExecution); /// /// Return the result, same as GetResult() /// TResult Result { get; } /// /// Returns the exception if occured otherwise returns null. /// object Exception { get; } } #endregion }