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