123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388 |
- using System;
- using System.Threading;
- namespace Amib.Threading.Internal
- {
- public abstract class WorkItemsGroupBase : IWorkItemsGroup
- {
- #region Private Fields
- /// <summary>
- /// Contains the name of this instance of SmartThreadPool.
- /// Can be changed by the user.
- /// </summary>
- private string _name = "WorkItemsGroupBase";
- public WorkItemsGroupBase()
- {
- IsIdle = true;
- }
- #endregion
- #region IWorkItemsGroup Members
- #region Public Methods
- /// <summary>
- /// Get/Set the name of the SmartThreadPool/WorkItemsGroup instance
- /// </summary>
- public string Name
- {
- get { return _name; }
- set { _name = value; }
- }
- public int localID { get; set;}
- #endregion
- #region Abstract Methods
- public abstract int Concurrency { get; set; }
- public abstract int WaitingCallbacks { get; }
- public abstract object[] GetStates();
- public abstract WIGStartInfo WIGStartInfo { get; }
- public abstract void Start();
- public abstract void Cancel(bool abortExecution);
- public abstract bool WaitForIdle(int millisecondsTimeout);
- public abstract event WorkItemsGroupIdleHandler OnIdle;
- internal abstract void Enqueue(WorkItem workItem);
- internal virtual void PreQueueWorkItem() { }
- #endregion
- #region Common Base Methods
- /// <summary>
- /// Cancel all the work items.
- /// Same as Cancel(false)
- /// </summary>
- public virtual void Cancel()
- {
- Cancel(false);
- }
- /// <summary>
- /// Wait for the SmartThreadPool/WorkItemsGroup to be idle
- /// </summary>
- public void WaitForIdle()
- {
- WaitForIdle(Timeout.Infinite);
- }
- /// <summary>
- /// Wait for the SmartThreadPool/WorkItemsGroup to be idle
- /// </summary>
- public bool WaitForIdle(TimeSpan timeout)
- {
- return WaitForIdle((int)timeout.TotalMilliseconds);
- }
- /// <summary>
- /// IsIdle is true when there are no work items running or queued.
- /// </summary>
- public bool IsIdle { get; protected set; }
- #endregion
- #region QueueWorkItem
- public IWorkItemResult QueueWorkItem(WaitCallback callback)
- {
- WorkItem workItem = WorkItemFactory.CreateWorkItem(this, WIGStartInfo, callback, null);
- Enqueue(workItem);
- return workItem.GetWorkItemResult();
- }
- public IWorkItemResult QueueWorkItem(WaitCallback callback, object state)
- {
- WorkItem workItem = WorkItemFactory.CreateWorkItem(this, WIGStartInfo, callback, state);
- Enqueue(workItem);
- return workItem.GetWorkItemResult();
- }
- public IWorkItemResult QueueWorkItem(WorkItemInfo workItemInfo, WaitCallback callback)
- {
- PreQueueWorkItem();
- WorkItem workItem = WorkItemFactory.CreateWorkItem(this, WIGStartInfo, workItemInfo, callback, null);
- Enqueue(workItem);
- return workItem.GetWorkItemResult();
- }
- public IWorkItemResult QueueWorkItem(WorkItemInfo workItemInfo, WaitCallback callback, object state)
- {
- PreQueueWorkItem();
- WorkItem workItem = WorkItemFactory.CreateWorkItem(this, WIGStartInfo, workItemInfo, callback, state);
- Enqueue(workItem);
- return workItem.GetWorkItemResult();
- }
- /// <summary>
- /// Queue a work item
- /// </summary>
- /// <param name="callback">A callback to execute</param>
- /// <returns>Returns a work item result</returns>
- public IWorkItemResult QueueWorkItem(WorkItemCallback callback)
- {
- WorkItem workItem = WorkItemFactory.CreateWorkItem(this, WIGStartInfo, callback);
- Enqueue(workItem);
- return workItem.GetWorkItemResult();
- }
- /// <summary>
- /// Queue a work item
- /// </summary>
- /// <param name="workItemInfo">Work item info</param>
- /// <param name="callback">A callback to execute</param>
- /// <returns>Returns a work item result</returns>
- public IWorkItemResult QueueWorkItem(WorkItemInfo workItemInfo, WorkItemCallback callback)
- {
- PreQueueWorkItem();
- WorkItem workItem = WorkItemFactory.CreateWorkItem(this, WIGStartInfo, workItemInfo, callback);
- Enqueue(workItem);
- return workItem.GetWorkItemResult();
- }
- /// <summary>
- /// Queue a work item
- /// </summary>
- /// <param name="callback">A callback to execute</param>
- /// <param name="state">
- /// The context object of the work item. Used for passing arguments to the work item.
- /// </param>
- /// <returns>Returns a work item result</returns>
- public IWorkItemResult QueueWorkItem(WorkItemCallback callback, object state)
- {
- WorkItem workItem = WorkItemFactory.CreateWorkItem(this, WIGStartInfo, callback, state);
- Enqueue(workItem);
- return workItem.GetWorkItemResult();
- }
- /// <summary>
- /// Queue a work item
- /// </summary>
- /// <param name="workItemInfo">Work item information</param>
- /// <param name="callback">A callback to execute</param>
- /// <param name="state">
- /// The context object of the work item. Used for passing arguments to the work item.
- /// </param>
- /// <returns>Returns a work item result</returns>
- public IWorkItemResult QueueWorkItem(WorkItemInfo workItemInfo, WorkItemCallback callback, object state)
- {
- PreQueueWorkItem();
- WorkItem workItem = WorkItemFactory.CreateWorkItem(this, WIGStartInfo, workItemInfo, callback, state);
- Enqueue(workItem);
- return workItem.GetWorkItemResult();
- }
- /// <summary>
- /// Queue a work item
- /// </summary>
- /// <param name="callback">A callback to execute</param>
- /// <param name="state">
- /// The context object of the work item. Used for passing arguments to the work item.
- /// </param>
- /// <param name="postExecuteWorkItemCallback">
- /// A delegate to call after the callback completion
- /// </param>
- /// <returns>Returns a work item result</returns>
- public IWorkItemResult QueueWorkItem( WorkItemCallback callback, object state,
- PostExecuteWorkItemCallback postExecuteWorkItemCallback)
- {
- PreQueueWorkItem();
- WorkItem workItem = WorkItemFactory.CreateWorkItem(this, WIGStartInfo, callback, state, postExecuteWorkItemCallback);
- Enqueue(workItem);
- return workItem.GetWorkItemResult();
- }
- /// <summary>
- /// Queue a work item
- /// </summary>
- /// <param name="callback">A callback to execute</param>
- /// <param name="state">
- /// The context object of the work item. Used for passing arguments to the work item.
- /// </param>
- /// <param name="postExecuteWorkItemCallback">
- /// A delegate to call after the callback completion
- /// </param>
- /// <param name="callToPostExecute">Indicates on which cases to call to the post execute callback</param>
- /// <returns>Returns a work item result</returns>
- public IWorkItemResult QueueWorkItem( WorkItemCallback callback, object state,
- PostExecuteWorkItemCallback postExecuteWorkItemCallback, CallToPostExecute callToPostExecute)
- {
- PreQueueWorkItem();
- WorkItem workItem = WorkItemFactory.CreateWorkItem(this, WIGStartInfo, callback, state, postExecuteWorkItemCallback, callToPostExecute);
- Enqueue(workItem);
- return workItem.GetWorkItemResult();
- }
- #endregion
- #region QueueWorkItem(Action<...>)
- public IWorkItemResult QueueWorkItem (Action action)
- {
- PreQueueWorkItem ();
- WorkItem workItem = WorkItemFactory.CreateWorkItem (
- this,
- WIGStartInfo,
- delegate
- {
- action.Invoke ();
- return null;
- });
- Enqueue (workItem);
- return workItem.GetWorkItemResult ();
- }
- public IWorkItemResult QueueWorkItem<T> (Action<T> action, T arg)
- {
- PreQueueWorkItem ();
- WorkItem workItem = WorkItemFactory.CreateWorkItem (
- this,
- WIGStartInfo,
- state =>
- {
- action.Invoke (arg);
- return null;
- },
- WIGStartInfo.FillStateWithArgs ? new object[] { arg } : null);
- Enqueue (workItem);
- return workItem.GetWorkItemResult ();
- }
- public IWorkItemResult QueueWorkItem<T1, T2> (Action<T1, T2> action, T1 arg1, T2 arg2)
- {
- PreQueueWorkItem ();
- WorkItem workItem = WorkItemFactory.CreateWorkItem (
- this,
- WIGStartInfo,
- state =>
- {
- action.Invoke (arg1, arg2);
- return null;
- },
- WIGStartInfo.FillStateWithArgs ? new object[] { arg1, arg2 } : null);
- Enqueue (workItem);
- return workItem.GetWorkItemResult ();
- }
- public IWorkItemResult QueueWorkItem<T1, T2, T3> (Action<T1, T2, T3> action, T1 arg1, T2 arg2, T3 arg3)
- {
- PreQueueWorkItem ();
- WorkItem workItem = WorkItemFactory.CreateWorkItem (
- this,
- WIGStartInfo,
- state =>
- {
- action.Invoke (arg1, arg2, arg3);
- return null;
- },
- WIGStartInfo.FillStateWithArgs ? new object[] { arg1, arg2, arg3 } : null);
- Enqueue (workItem);
- return workItem.GetWorkItemResult ();
- }
- public IWorkItemResult QueueWorkItem<T1, T2, T3, T4> (
- Action<T1, T2, T3, T4> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4)
- {
- PreQueueWorkItem ();
- WorkItem workItem = WorkItemFactory.CreateWorkItem (
- this,
- WIGStartInfo,
- state =>
- {
- action.Invoke (arg1, arg2, arg3, arg4);
- return null;
- },
- WIGStartInfo.FillStateWithArgs ? new object[] { arg1, arg2, arg3, arg4 } : null);
- Enqueue (workItem);
- return workItem.GetWorkItemResult ();
- }
- #endregion
- #region QueueWorkItem(Func<...>)
- public IWorkItemResult<TResult> QueueWorkItem<TResult>(Func<TResult> func)
- {
- PreQueueWorkItem();
- WorkItem workItem = WorkItemFactory.CreateWorkItem(
- this,
- WIGStartInfo,
- state =>
- {
- return func.Invoke();
- });
- Enqueue(workItem);
- return new WorkItemResultTWrapper<TResult>(workItem.GetWorkItemResult());
- }
- public IWorkItemResult<TResult> QueueWorkItem<T, TResult>(Func<T, TResult> func, T arg)
- {
- PreQueueWorkItem();
- WorkItem workItem = WorkItemFactory.CreateWorkItem(
- this,
- WIGStartInfo,
- state =>
- {
- return func.Invoke(arg);
- },
- WIGStartInfo.FillStateWithArgs ? new object[] { arg } : null);
- Enqueue(workItem);
- return new WorkItemResultTWrapper<TResult>(workItem.GetWorkItemResult());
- }
- public IWorkItemResult<TResult> QueueWorkItem<T1, T2, TResult>(Func<T1, T2, TResult> func, T1 arg1, T2 arg2)
- {
- PreQueueWorkItem();
- WorkItem workItem = WorkItemFactory.CreateWorkItem(
- this,
- WIGStartInfo,
- state =>
- {
- return func.Invoke(arg1, arg2);
- },
- WIGStartInfo.FillStateWithArgs ? new object[] { arg1, arg2 } : null);
- Enqueue(workItem);
- return new WorkItemResultTWrapper<TResult>(workItem.GetWorkItemResult());
- }
- public IWorkItemResult<TResult> QueueWorkItem<T1, T2, T3, TResult>(
- Func<T1, T2, T3, TResult> func, T1 arg1, T2 arg2, T3 arg3)
- {
- PreQueueWorkItem();
- WorkItem workItem = WorkItemFactory.CreateWorkItem(
- this,
- WIGStartInfo,
- state =>
- {
- return func.Invoke(arg1, arg2, arg3);
- },
- WIGStartInfo.FillStateWithArgs ? new object[] { arg1, arg2, arg3 } : null);
- Enqueue(workItem);
- return new WorkItemResultTWrapper<TResult>(workItem.GetWorkItemResult());
- }
- public IWorkItemResult<TResult> QueueWorkItem<T1, T2, T3, T4, TResult>(
- Func<T1, T2, T3, T4, TResult> func, T1 arg1, T2 arg2, T3 arg3, T4 arg4)
- {
- PreQueueWorkItem();
- WorkItem workItem = WorkItemFactory.CreateWorkItem(
- this,
- WIGStartInfo,
- state =>
- {
- return func.Invoke(arg1, arg2, arg3, arg4);
- },
- WIGStartInfo.FillStateWithArgs ? new object[] { arg1, arg2, arg3, arg4 } : null);
- Enqueue(workItem);
- return new WorkItemResultTWrapper<TResult>(workItem.GetWorkItemResult());
- }
- #endregion
- #endregion
- }
- }
|