/// <summary> /// Example of how to use the post execute callback /// </summary> private bool DoTestDefaultPostExecute(CallToPostExecute callToPostExecute, bool answer) { SmartThreadPool smartThreadPool = new SmartThreadPool(); WIGStartInfo wigStartInfo = new WIGStartInfo(); wigStartInfo.CallToPostExecute = callToPostExecute; wigStartInfo.PostExecuteWorkItemCallback = new PostExecuteWorkItemCallback(this.DoSomePostExecuteWork); IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(int.MaxValue, wigStartInfo); bool success = false; PostExecuteResult postExecuteResult = new PostExecuteResult(); IWorkItemResult wir = workItemsGroup.QueueWorkItem( new WorkItemCallback(this.DoSomeWork), postExecuteResult); if (!wir.IsCompleted) { int result = (int)wir.GetResult(); success = (1 == result); success = success && (postExecuteResult.wh.WaitOne(1000, true) == answer); } smartThreadPool.Shutdown(); return(success); }
/// <summary> /// Create a new work item /// </summary> /// <param name="workItemsGroup">The work items group</param> /// <param name="wigStartInfo">Work item group start 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> /// <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</returns> public static WorkItem CreateWorkItem( IWorkItemsGroup workItemsGroup, WIGStartInfo wigStartInfo, WorkItemCallback callback, object state, PostExecuteWorkItemCallback postExecuteWorkItemCallback, CallToPostExecute callToPostExecute) { ValidateCallback(callback); ValidateCallback(postExecuteWorkItemCallback); if (null == wigStartInfo) { throw new ArgumentNullException("wigStartInfo"); } WorkItemInfo workItemInfo = new WorkItemInfo(); workItemInfo.UseCallerCallContext = wigStartInfo.UseCallerCallContext; workItemInfo.UseCallerHttpContext = wigStartInfo.UseCallerHttpContext; workItemInfo.PostExecuteWorkItemCallback = postExecuteWorkItemCallback; workItemInfo.CallToPostExecute = callToPostExecute; workItemInfo.DisposeOfStateObjects = wigStartInfo.DisposeOfStateObjects; workItemInfo.WorkItemPriority = wigStartInfo.WorkItemPriority; WorkItem workItem = new WorkItem( workItemsGroup, workItemInfo, callback, state); return(workItem); }
/// <summary> /// Create a new work item /// </summary> /// <param name="workItemsGroup">The work items group</param> /// <param name="wigStartInfo">Work item group start 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> /// <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> /// <param name="workItemPriority">The work item priority</param> /// <returns>Returns a work item</returns> public static WorkItem CreateWorkItem( IWorkItemsGroup workItemsGroup, WIGStartInfo wigStartInfo, WorkItemCallback callback, object state, PostExecuteWorkItemCallback postExecuteWorkItemCallback, CallToPostExecute callToPostExecute, WorkItemPriority workItemPriority) { ValidateCallback(callback); ValidateCallback(postExecuteWorkItemCallback); WorkItemInfo workItemInfo = new WorkItemInfo(); workItemInfo.UseCallerCallContext = wigStartInfo.UseCallerCallContext; workItemInfo.UseCallerHttpContext = wigStartInfo.UseCallerHttpContext; workItemInfo.PostExecuteWorkItemCallback = postExecuteWorkItemCallback; workItemInfo.CallToPostExecute = callToPostExecute; workItemInfo.WorkItemPriority = workItemPriority; workItemInfo.DisposeOfStateObjects = wigStartInfo.DisposeOfStateObjects; WorkItem workItem = new WorkItem( workItemsGroup, workItemInfo, callback, state); return(workItem); }
/// <summary> /// Execute the work item and the post execute /// </summary> public void Execute() { CallToPostExecute currentCallToPostExecute = 0; // Execute the work item if we are in the correct state switch (GetWorkItemState()) { case WorkItemState.InProgress: currentCallToPostExecute |= CallToPostExecute.WhenWorkItemNotCanceled; ExecuteWorkItem(); break; case WorkItemState.Canceled: currentCallToPostExecute |= CallToPostExecute.WhenWorkItemCanceled; break; default: Debug.Assert(false); throw new NotSupportedException(); } // Run the post execute as needed if ((currentCallToPostExecute & _workItemInfo.CallToPostExecute) != 0) { PostExecute(); } _processingStopwatch.Stop(); }
/// <summary> /// Example of how to use the post execute callback /// </summary> private bool DoTestPostExecute(CallToPostExecute callToPostExecute, bool answer) { SmartThreadPool smartThreadPool = new SmartThreadPool(); bool success = false; PostExecuteResult postExecuteResult = new PostExecuteResult(); IWorkItemResult wir = smartThreadPool.QueueWorkItem( new WorkItemCallback(this.DoSomeWork), postExecuteResult, new PostExecuteWorkItemCallback(this.DoSomePostExecuteWork), callToPostExecute); if (!wir.IsCompleted) { int result = (int)wir.GetResult(); success = (1 == result); success = success && (postExecuteResult.wh.WaitOne(1000, true) == answer); } smartThreadPool.Shutdown(); return(success); }
public WorkItemInfo() { this._useCallerCallContext = false; this._useCallerHttpContext = false; this._disposeOfStateObjects = false; this._callToPostExecute = CallToPostExecute.Always; this._postExecuteWorkItemCallback = SmartThreadPool.DefaultPostExecuteWorkItemCallback; this._workItemPriority = WorkItemPriority.Normal; }
public WorkItemInfo() { _useCallerCallContext = SmartThreadPool.DefaultUseCallerCallContext; _useCallerHttpContext = SmartThreadPool.DefaultUseCallerHttpContext; _disposeOfStateObjects = SmartThreadPool.DefaultDisposeOfStateObjects; _callToPostExecute = SmartThreadPool.DefaultCallToPostExecute; _postExecuteWorkItemCallback = SmartThreadPool.DefaultPostExecuteWorkItemCallback; _workItemPriority = SmartThreadPool.DefaultWorkItemPriority; }
public WorkItemInfo(WorkItemInfo workItemInfo) { _useCallerCallContext = workItemInfo._useCallerCallContext; _useCallerHttpContext = workItemInfo._useCallerHttpContext; _disposeOfStateObjects = workItemInfo._disposeOfStateObjects; _callToPostExecute = workItemInfo._callToPostExecute; _postExecuteWorkItemCallback = workItemInfo._postExecuteWorkItemCallback; _workItemPriority = workItemInfo._workItemPriority; }
public WIGStartInfo() { _useCallerCallContext = SmartThreadPool.DefaultUseCallerCallContext; _useCallerHttpContext = SmartThreadPool.DefaultUseCallerHttpContext; _disposeOfStateObjects = SmartThreadPool.DefaultDisposeOfStateObjects; _callToPostExecute = SmartThreadPool.DefaultCallToPostExecute; _postExecuteWorkItemCallback = SmartThreadPool.DefaultPostExecuteWorkItemCallback; _startSuspended = SmartThreadPool.DefaultStartSuspended; }
public WIGStartInfo(WIGStartInfo wigStartInfo) { _useCallerCallContext = wigStartInfo._useCallerCallContext; _useCallerHttpContext = wigStartInfo._useCallerHttpContext; _disposeOfStateObjects = wigStartInfo._disposeOfStateObjects; _callToPostExecute = wigStartInfo._callToPostExecute; _postExecuteWorkItemCallback = wigStartInfo._postExecuteWorkItemCallback; _startSuspended = wigStartInfo._startSuspended; }
public WIGStartInfo() { this._useCallerCallContext = false; this._useCallerHttpContext = false; this._disposeOfStateObjects = false; this._callToPostExecute = CallToPostExecute.Always; this._postExecuteWorkItemCallback = SmartThreadPool.DefaultPostExecuteWorkItemCallback; this._startSuspended = false; }
public WIGStartInfo(WIGStartInfo wigStartInfo) { _useCallerCallContext = wigStartInfo.UseCallerCallContext; _disposeOfStateObjects = wigStartInfo.DisposeOfStateObjects; _callToPostExecute = wigStartInfo.CallToPostExecute; _postExecuteWorkItemCallback = wigStartInfo.PostExecuteWorkItemCallback; _startSuspended = wigStartInfo.StartSuspended; _fillStateWithArgs = wigStartInfo.FillStateWithArgs; }
public WIGStartInfo() { _fillStateWithArgs = SmartThreadPool.DefaultFillStateWithArgs; _workItemPriority = WorkItemPriority.Normal; _startSuspended = SmartThreadPool.DefaultStartSuspended; _postExecuteWorkItemCallback = SmartThreadPool.DefaultPostExecuteWorkItemCallback; _callToPostExecute = SmartThreadPool.DefaultCallToPostExecute; _disposeOfStateObjects = SmartThreadPool.DefaultDisposeOfStateObjects; _useCallerCallContext = SmartThreadPool.DefaultUseCallerCallContext; }
// Token: 0x06001837 RID: 6199 // RVA: 0x00074E5C File Offset: 0x0007305C public WIGStartInfo(WIGStartInfo wigstartInfo_0) { this._useCallerCallContext = wigstartInfo_0.UseCallerCallContext; this._useCallerHttpContext = wigstartInfo_0.UseCallerHttpContext; this._disposeOfStateObjects = wigstartInfo_0.DisposeOfStateObjects; this._callToPostExecute = wigstartInfo_0.CallToPostExecute; this._postExecuteWorkItemCallback = wigstartInfo_0.PostExecuteWorkItemCallback; this._workItemPriority = wigstartInfo_0.WorkItemPriority; this._startSuspended = wigstartInfo_0.StartSuspended; this._fillStateWithArgs = wigstartInfo_0.FillStateWithArgs; }
// Token: 0x06001836 RID: 6198 // RVA: 0x00074E0C File Offset: 0x0007300C public WIGStartInfo() { this._fillStateWithArgs = false; this._workItemPriority = WorkItemPriority.Normal; this._startSuspended = false; this._postExecuteWorkItemCallback = SmartThreadPool.DefaultPostExecuteWorkItemCallback; this._callToPostExecute = CallToPostExecute.Always; this._disposeOfStateObjects = false; this._useCallerHttpContext = false; this._useCallerCallContext = false; }
public WIGStartInfo(WIGStartInfo wigStartInfo) { _useCallerCallContext = wigStartInfo.UseCallerCallContext; _useCallerHttpContext = wigStartInfo.UseCallerHttpContext; _disposeOfStateObjects = wigStartInfo.DisposeOfStateObjects; _callToPostExecute = wigStartInfo.CallToPostExecute; _postExecuteWorkItemCallback = wigStartInfo.PostExecuteWorkItemCallback; _workItemPriority = wigStartInfo.WorkItemPriority; _startSuspended = wigStartInfo.StartSuspended; _fillStateWithArgs = wigStartInfo.FillStateWithArgs; }
/// <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) { WorkItem workItem = WorkItemFactory.CreateWorkItem(this, WIGStartInfo, callback, state, postExecuteWorkItemCallback, callToPostExecute); Enqueue(workItem); return(workItem.GetWorkItemResult()); }
public WIGStartInfo(WIGStartInfo wigStartInfo) { this._useCallerCallContext = wigStartInfo.UseCallerCallContext; this._useCallerHttpContext = wigStartInfo.UseCallerHttpContext; this._disposeOfStateObjects = wigStartInfo.DisposeOfStateObjects; this._callToPostExecute = wigStartInfo.CallToPostExecute; this._postExecuteWorkItemCallback = wigStartInfo.PostExecuteWorkItemCallback; this._workItemPriority = wigStartInfo.WorkItemPriority; this._startSuspended = wigStartInfo.StartSuspended; this._fillStateWithArgs = wigStartInfo.FillStateWithArgs; }
/// <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> /// <param name="workItemPriority">The work item priority</param> /// <returns>Returns a work item result</returns> public IWorkItemResult QueueWorkItem( WorkItemCallback callback, object state, PostExecuteWorkItemCallback postExecuteWorkItemCallback, CallToPostExecute callToPostExecute, WorkItemPriority workItemPriority) { WorkItem workItem = WorkItemFactory.CreateWorkItem(this, _workItemsGroupStartInfo, callback, state, postExecuteWorkItemCallback, callToPostExecute, workItemPriority); EnqueueToSTPNextWorkItem(workItem); return(workItem.GetWorkItemResult()); }
/// <summary> /// /// </summary> /// <param name="wigStartInfo"></param> public WIGStartInfo(WIGStartInfo wigStartInfo) { if (null == wigStartInfo) { throw new ArgumentNullException("wigStartInfo"); } _useCallerCallContext = wigStartInfo.UseCallerCallContext; _useCallerHttpContext = wigStartInfo.UseCallerHttpContext; _disposeOfStateObjects = wigStartInfo.DisposeOfStateObjects; _callToPostExecute = wigStartInfo.CallToPostExecute; _postExecuteWorkItemCallback = wigStartInfo.PostExecuteWorkItemCallback; _workItemPriority = wigStartInfo.WorkItemPriority; _startSuspended = wigStartInfo.StartSuspended; _fillStateWithArgs = wigStartInfo.FillStateWithArgs; }
/// <summary> /// Queues a user work item to the thread pool. /// </summary> /// <param name="callback"> /// A WaitCallback representing the delegate to invoke when the thread in the /// thread pool picks up the work item. /// </param> /// <param name="state"> /// The object that is passed to the delegate when serviced from the thread pool. /// </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>Work item result</returns> public IWorkItemResult QueueWorkItem( WorkItemCallback callback, object state, PostExecuteWorkItemCallback postExecuteWorkItemCallback, CallToPostExecute callToPostExecute) { ValidateNotDisposed(); // Create a work item that contains the delegate and its state. WorkItem workItem = new WorkItem( callback, state, _useCallerContext, postExecuteWorkItemCallback, callToPostExecute); Enqueue(workItem); return(workItem.GetWorkItemResult()); }
/// <summary> /// Initialize the callback holding object. /// </summary> /// <param name="callback">Callback delegate for the callback.</param> /// <param name="state">State with which to call the callback delegate.</param> /// /// We assume that the WorkItem object is created within the thread /// that meant to run the callback public WorkItem( WorkItemCallback callback, object state, bool useCallerContext, PostExecuteWorkItemCallback postExecuteWorkItemCallback, CallToPostExecute callToPostExecute) { if (useCallerContext) { _callerContext = ChoCallerThreadContext.Capture(); } _postExecuteWorkItemCallback = postExecuteWorkItemCallback; _callToPostExecute = callToPostExecute; _callback = callback; _state = state; _workItemState = WorkItemState.InQueue; _workItemCompleted = null; _workItemCompletedRefCount = 0; _workItemResult = new WorkItemResult(this); }
public void Execute() { CallToPostExecute callToPostExecute = CallToPostExecute.Never; switch (this.GetWorkItemState()) { case WorkItem.WorkItemState.InProgress: callToPostExecute |= CallToPostExecute.WhenWorkItemNotCanceled; this.ExecuteWorkItem(); goto IL_37; case WorkItem.WorkItemState.Canceled: callToPostExecute |= CallToPostExecute.WhenWorkItemCanceled; goto IL_37; } throw new NotSupportedException(); IL_37: if ((callToPostExecute & this._workItemInfo.CallToPostExecute) != CallToPostExecute.Never) { this.PostExecute(); } this._processingStopwatch.Stop(); }
/// <summary> /// Example of how to queue a work item and then cancel it while it is in the queue. /// </summary> private bool DoTestPostExecuteWithCancel(CallToPostExecute callToPostExecute, bool answer) { // Create a SmartThreadPool with only one thread. // It just to show how to use the work item canceling feature SmartThreadPool smartThreadPool = new SmartThreadPool(); IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(1); bool success = false; PostExecuteResult postExecuteResult = new PostExecuteResult(); // Queue a work item that will occupy the thread in the pool workItemsGroup.QueueWorkItem( new WorkItemCallback(this.DoSomeWork), null); // Queue another work item that will wait for the first to complete IWorkItemResult wir = workItemsGroup.QueueWorkItem( new WorkItemCallback(this.DoSomeWork), postExecuteResult, new PostExecuteWorkItemCallback(this.DoSomePostExecuteWork), callToPostExecute); // Wait a while for the thread pool to start executing the first work item Thread.Sleep(100); // Cancel the second work item while it still in the queue if (wir.Cancel()) { success = (postExecuteResult.wh.WaitOne(1000, true) == answer); } smartThreadPool.Shutdown(); return(success); }
/// <summary> /// Execute the work item and the post execute /// </summary> public void Execute() { CallToPostExecute never = CallToPostExecute.Never; switch (this.GetWorkItemState()) { case WorkItemState.InProgress: never |= CallToPostExecute.WhenWorkItemNotCanceled; this.ExecuteWorkItem(); break; case WorkItemState.Canceled: never |= CallToPostExecute.WhenWorkItemCanceled; break; default: throw new NotSupportedException(); } if ((never & this._workItemInfo.CallToPostExecute) != CallToPostExecute.Never) { this.PostExecute(); } this._endProcessTime = DateTime.Now; }
private static void Load(NameValueCollection nameValues) { if (nameValues != null) { try { MaxWorkerThreads = Int32.Parse(nameValues["MaxWorkerThreads"]); } catch { } try { MinWorkerThreads = Int32.Parse(nameValues["MinWorkerThreads"]); } catch { } try { int idleTimeout = Int32.Parse(nameValues["IdleTimeoutInSeconds"]); if (idleTimeout >= 0) { IdleTimeout = idleTimeout * 1000; } } catch { } try { int workItemTimeout = Int32.Parse(nameValues["WorkItemTimeout"]); if (workItemTimeout > 0) { WorkItemTimeout = workItemTimeout; } else { WorkItemTimeout = -1; } } catch { } try { DisposeOfStateObjects = Boolean.Parse(nameValues["DisposeOfStateObjects"]); } catch { } try { UseCallerContext = Boolean.Parse(nameValues["UseCallerContext"]); } catch { } try { BlockIfPoolBusy = Boolean.Parse(nameValues["BlockIfPoolBusy"]); } catch { } try { if (nameValues["CallToPostExecute"] != null) { CallToPostExecute = (CallToPostExecute)Enum.Parse(typeof(CallToPostExecute), nameValues["CallToPostExecute"]); } } catch { } Validate(); } }
/// <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> /// <param name="workItemPriority">The work item priority</param> /// <returns>Returns a work item result</returns> public IWorkItemResult QueueWorkItem( WorkItemCallback callback, object state, PostExecuteWorkItemCallback postExecuteWorkItemCallback, CallToPostExecute callToPostExecute, WorkItemPriority workItemPriority) { WorkItem workItem = WorkItemFactory.CreateWorkItem(this, _workItemsGroupStartInfo, callback, state, postExecuteWorkItemCallback, callToPostExecute, workItemPriority); EnqueueToSTPNextWorkItem(workItem); return workItem.GetWorkItemResult(); }
public IWorkItemResult QueueWorkItem(WorkItemCallback callback, object state, PostExecuteWorkItemCallback postExecuteWorkItemCallback, CallToPostExecute callToPostExecute, WorkItemPriority workItemPriority) { this.PreQueueWorkItem(); WorkItem workItem = WorkItemFactory.CreateWorkItem(this, this.WIGStartInfo, callback, state, postExecuteWorkItemCallback, callToPostExecute, workItemPriority); this.Enqueue(workItem); return(workItem.GetWorkItemResult()); }
/// <summary> /// Example of how to use the post execute callback /// </summary> private bool DoTestPostExecute(CallToPostExecute callToPostExecute, bool answer) { SmartThreadPool smartThreadPool = new SmartThreadPool(); bool success = false; PostExecuteResult postExecuteResult = new PostExecuteResult(); IWorkItemResult wir = smartThreadPool.QueueWorkItem( new WorkItemCallback(this.DoSomeWork), postExecuteResult, new PostExecuteWorkItemCallback(this.DoSomePostExecuteWork), callToPostExecute); if (!wir.IsCompleted) { int result = (int)wir.GetResult(); success = (1 == result); success = success && (postExecuteResult.wh.WaitOne(1000, true) == answer); } smartThreadPool.Shutdown(); return success; }
/// <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> /// <param name="workItemPriority">The work item priority</param> /// <returns>Returns a work item result</returns> public IWorkItemResult QueueWorkItem( WorkItemCallback callback, object state, PostExecuteWorkItemCallback postExecuteWorkItemCallback, CallToPostExecute callToPostExecute, WorkItemPriority workItemPriority) { ValidateNotDisposed(); ValidateCallback(callback); WorkItem workItem = WorkItemFactory.CreateWorkItem(this, _stpStartInfo, callback, state, postExecuteWorkItemCallback, callToPostExecute, workItemPriority); 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> /// <param name="workItemPriority">The work item priority</param> /// <returns>Returns a work item result</returns> public IWorkItemResult QueueWorkItem(WorkItemCallback callback, object state, PostExecuteWorkItemCallback postExecuteWorkItemCallback, CallToPostExecute callToPostExecute, WorkItemPriority workItemPriority) { this.ValidateNotDisposed(); this.ValidateCallback(callback); WorkItem workItem = WorkItemFactory.CreateWorkItem(this, this._stpStartInfo, callback, state, postExecuteWorkItemCallback, callToPostExecute, workItemPriority); this.Enqueue(workItem); return(workItem.GetWorkItemResult()); }
/// <summary> /// Create a new work item /// </summary> /// <param name="workItemsGroup"></param> /// <param name="wigStartInfo">Work item group start 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> /// <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> /// <param name="workItemPriority">The work item priority</param> /// <returns>Returns a work item</returns> public static WorkItem CreateWorkItem(IWorkItemsGroup workItemsGroup, WIGStartInfo wigStartInfo, WorkItemCallback callback, object state, PostExecuteWorkItemCallback postExecuteWorkItemCallback, CallToPostExecute callToPostExecute, WorkItemPriority workItemPriority) { ValidateCallback(callback); ValidateCallback(postExecuteWorkItemCallback); return(new WorkItem(workItemsGroup, new WorkItemInfo { UseCallerCallContext = wigStartInfo.UseCallerCallContext, UseCallerHttpContext = wigStartInfo.UseCallerHttpContext, PostExecuteWorkItemCallback = postExecuteWorkItemCallback, CallToPostExecute = callToPostExecute, WorkItemPriority = workItemPriority, DisposeOfStateObjects = wigStartInfo.DisposeOfStateObjects }, callback, state)); }
/// <summary> /// Example of how to use the post execute callback /// </summary> private bool DoTestDefaultPostExecute(CallToPostExecute callToPostExecute, bool answer) { SmartThreadPool smartThreadPool = new SmartThreadPool(); WIGStartInfo wigStartInfo = new WIGStartInfo(); wigStartInfo.CallToPostExecute = callToPostExecute; wigStartInfo.PostExecuteWorkItemCallback = new PostExecuteWorkItemCallback(this.DoSomePostExecuteWork); IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(int.MaxValue, wigStartInfo); bool success = false; PostExecuteResult postExecuteResult = new PostExecuteResult(); IWorkItemResult wir = workItemsGroup.QueueWorkItem( new WorkItemCallback(this.DoSomeWork), postExecuteResult); if (!wir.IsCompleted) { int result = (int)wir.GetResult(); success = (1 == result); success = success && (postExecuteResult.wh.WaitOne(1000, true) == answer); } smartThreadPool.Shutdown(); return success; }
/// <summary> /// Create a new work item /// </summary> /// <param name = "wigStartInfo">Work item group start 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> /// <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> /// <param name = "workItemPriority">The work item priority</param> /// <returns>Returns a work item</returns> public static WorkItem CreateWorkItem( IWorkItemsGroup workItemsGroup, WIGStartInfo wigStartInfo, WorkItemCallback callback, object state, PostExecuteWorkItemCallback postExecuteWorkItemCallback, CallToPostExecute callToPostExecute, WorkItemPriority workItemPriority) { ValidateCallback(callback); ValidateCallback(postExecuteWorkItemCallback); WorkItemInfo workItemInfo = new WorkItemInfo { UseCallerCallContext = wigStartInfo.UseCallerCallContext, UseCallerHttpContext = wigStartInfo.UseCallerHttpContext, PostExecuteWorkItemCallback = postExecuteWorkItemCallback, CallToPostExecute = callToPostExecute, WorkItemPriority = workItemPriority, DisposeOfStateObjects = wigStartInfo.DisposeOfStateObjects }; WorkItem workItem = new WorkItem( workItemsGroup, workItemInfo, callback, state); return workItem; }
/// <summary> /// Example of how to queue a work item and then cancel it while it is in the queue. /// </summary> private bool DoTestPostExecuteWithCancel(CallToPostExecute callToPostExecute, bool answer) { // Create a SmartThreadPool with only one thread. // It just to show how to use the work item canceling feature SmartThreadPool smartThreadPool = new SmartThreadPool(10*1000, 1); bool success = false; PostExecuteResult postExecuteResult = new PostExecuteResult(); // Queue a work item that will occupy the thread in the pool smartThreadPool.QueueWorkItem( new WorkItemCallback(this.DoSomeWork), null); // Queue another work item that will wait for the first to complete IWorkItemResult wir = smartThreadPool.QueueWorkItem( new WorkItemCallback(this.DoSomeWork), postExecuteResult, new PostExecuteWorkItemCallback(this.DoSomePostExecuteWork), callToPostExecute); // Wait a while for the thread pool to start executing the first work item Thread.Sleep(100); // Cancel the second work item while it still in the queue if (wir.Cancel()) { success = (postExecuteResult.wh.WaitOne(1000, true) == answer); } smartThreadPool.Shutdown(); return success; }
/// <summary> /// Create a new work item /// </summary> /// <param name="wigStartInfo">Work item group start 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> /// <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</returns> public static WorkItem CreateWorkItem( IWorkItemsGroup workItemsGroup, WIGStartInfo wigStartInfo, WorkItemCallback callback, object state, PostExecuteWorkItemCallback postExecuteWorkItemCallback, CallToPostExecute callToPostExecute) { ValidateCallback(callback); ValidateCallback(postExecuteWorkItemCallback); var workItemInfo = new WorkItemInfo(); workItemInfo.UseCallerCallContext = wigStartInfo.UseCallerCallContext; workItemInfo.UseCallerHttpContext = wigStartInfo.UseCallerHttpContext; workItemInfo.PostExecuteWorkItemCallback = postExecuteWorkItemCallback; workItemInfo.CallToPostExecute = callToPostExecute; workItemInfo.DisposeOfStateObjects = wigStartInfo.DisposeOfStateObjects; var workItem = new WorkItem( workItemsGroup, workItemInfo, callback, state); return workItem; }