public void CancelCancelledWorkItemAbort() { ManualResetEvent waitToStart = new ManualResetEvent(false); SmartThreadPool stp = new SmartThreadPool(); IWorkItemResult wir = stp.QueueWorkItem( state => { waitToStart.Set(); while (true) { Thread.Sleep(1000); } return(null); } ); waitToStart.WaitOne(); wir.Cancel(false); Assert.IsTrue(wir.IsCanceled); bool completed = stp.WaitForIdle(1000); Assert.IsFalse(completed); wir.Cancel(true); try { wir.GetResult(); } finally { stp.Shutdown(); } }
public void Stop() { try { if (!WorkItem.Cancel()) { WorkItem.Cancel(true); } } catch (Exception) { } }
public void CancelInProgressWorkItemAbort() { ManualResetEvent waitToStart = new ManualResetEvent(false); int counter = 0; SmartThreadPool stp = new SmartThreadPool(); IWorkItemResult wir = stp.QueueWorkItem( state => { waitToStart.Set(); Thread.Sleep(100); ++counter; return(null); } ); waitToStart.WaitOne(); wir.Cancel(true); Assert.IsTrue(wir.IsCanceled); Assert.AreEqual(counter, 0); try { wir.GetResult(); } finally { stp.Shutdown(); } }
public void CancelInProgressWorkItemSoftWithSample() { bool cancelled = false; ManualResetEvent waitToStart = new ManualResetEvent(false); ManualResetEvent waitToComplete = new ManualResetEvent(false); SmartThreadPool stp = new SmartThreadPool(); IWorkItemResult wir = stp.QueueWorkItem( state => { waitToStart.Set(); waitToComplete.WaitOne(); cancelled = SmartThreadPool.IsWorkItemCanceled; return(null); } ); waitToStart.WaitOne(); wir.Cancel(false); waitToComplete.Set(); stp.WaitForIdle(); Assert.IsTrue(cancelled); stp.Shutdown(); }
public void CancelInProgressWorkItemSoftWithIgnoreSample() { Assert.Throws <WorkItemCancelException>(() => { ManualResetEvent waitToStart = new ManualResetEvent(false); ManualResetEvent waitToComplete = new ManualResetEvent(false); SmartThreadPool stp = new SmartThreadPool(); IWorkItemResult wir = stp.QueueWorkItem( state => { waitToStart.Set(); Thread.Sleep(100); waitToComplete.WaitOne(); return(null); } ); waitToStart.WaitOne(); wir.Cancel(false); waitToComplete.Set(); stp.WaitForIdle(); // Throws WorkItemCancelException wir.GetResult(); stp.Shutdown(); }); }
public void CancelInProgressWorkItemSoftWithAbortOnWorkItemCancel() { bool abortFailed = false; ManualResetEvent waitToStart = new ManualResetEvent(false); ManualResetEvent waitToCancel = new ManualResetEvent(false); SmartThreadPool stp = new SmartThreadPool(); IWorkItemResult wir = stp.QueueWorkItem( state => { waitToStart.Set(); waitToCancel.WaitOne(); SmartThreadPool.AbortOnWorkItemCancel(); abortFailed = true; return(null); }); waitToStart.WaitOne(); wir.Cancel(false); waitToCancel.Set(); stp.WaitForIdle(); Assert.IsTrue(wir.IsCanceled); Assert.IsFalse(abortFailed); stp.Shutdown(); }
//[ExpectedException(typeof(WorkItemCancelException))] public void CancelInProgressWorkItemSoft() { Assert.Throws <WorkItemCancelException>(() => { ManualResetEvent waitToStart = new ManualResetEvent(false); SmartThreadPool stp = new SmartThreadPool(); IWorkItemResult wir = stp.QueueWorkItem( state => { waitToStart.Set(); Thread.Sleep(100); return(null); } ); waitToStart.WaitOne(); wir.Cancel(false); Assert.IsTrue(wir.IsCanceled); try { wir.GetResult(); } finally { stp.Shutdown(); } }); }
//[ExpectedException(typeof(WorkItemCancelException))] public void CancelCancelledWorkItemAbort() { Assert.ThrowsException <WorkItemCancelException>(() => { ManualResetEvent waitToStart = new ManualResetEvent(false); STP stp = new STP(); IWorkItemResult wir = stp.QueueWorkItem( state => { waitToStart.Set(); while (true) { Thread.Sleep(1000); } #pragma warning disable CS0162 // Unreachable code detected return(null); #pragma warning restore CS0162 // Unreachable code detected } ); waitToStart.WaitOne(); wir.Cancel(false); Assert.IsTrue(wir.IsCanceled); bool completed = stp.WaitForIdle(1000); Assert.IsFalse(completed); wir.Cancel(true); try { wir.GetResult(); } finally { stp.Shutdown(); } }); }
public void Stop() { try { Removed = true; if (WorkItem == null) { return; } if (!WorkItem.Cancel()) { WorkItem.Cancel(true); } } catch (Exception) { } }
protected override void Dispose(bool isDisposing) { wasDisposed = true; runningThread.Cancel(); while (!runningThread.IsCompleted) { Thread.Sleep(50); } GameBase.BackgroundLoading = false; base.Dispose(isDisposing); }
public void CancelCanceledWorkItem() { Assert.ThrowsException <WorkItemCancelException>(() => { STPStartInfo stpStartInfo = new STPStartInfo { StartSuspended = true }; STP stp = new STP(stpStartInfo); IWorkItemResult wir = stp.QueueWorkItem(state => null); int counter = 0; wir.Cancel(); try { wir.GetResult(); } catch (WorkItemCancelException ce) { ce.GetHashCode(); ++counter; } Assert.AreEqual(counter, 1); wir.Cancel(); try { wir.GetResult(); } finally { stp.Shutdown(); } }); }
public void Cancel() { try { // sincronizamos este flag lock (syncRoot) { if (thread != null && !isCompleted) { workItemResult.Cancel(); } } } catch { } }
public void CancelCanceledWorkItem() { STPStartInfo stpStartInfo = new STPStartInfo(); stpStartInfo.StartSuspended = true; SmartThreadPool stp = new SmartThreadPool(stpStartInfo); IWorkItemResult wir = stp.QueueWorkItem(state => null); int counter = 0; wir.Cancel(); try { wir.GetResult(); } catch (WorkItemCancelException ce) { ce.GetHashCode(); ++counter; } Assert.AreEqual(counter, 1); wir.Cancel(); try { wir.GetResult(); } finally { stp.Shutdown(); } }
public void CancelCompletedWorkItem() { SmartThreadPool stp = new SmartThreadPool(); IWorkItemResult wir = stp.QueueWorkItem( state => 1 ); stp.WaitForIdle(); Assert.AreEqual(wir.GetResult(), 1); wir.Cancel(); Assert.AreEqual(wir.GetResult(), 1); stp.Shutdown(); }
public void DoWork(object state) { SmartThreadPool smartThreadPool = new SmartThreadPool(); // Queue the work item IWorkItemResult wir = smartThreadPool.QueueWorkItem(DoRealWork); // Give the work item some time to complete. Thread.Sleep(1000); // If the work item hasn't completed yet then cancel it. if (!wir.IsCompleted) { wir.Cancel(); } smartThreadPool.Shutdown(); }
/// <summary> /// Forcefully abort the request. /// </summary> public void Abort() { if (Aborted) { return; } Aborted = true; abortRequest(); workItem?.Cancel(); workItem = null; if (!KeepEventsBound) { unbindEvents(); } }
public void CancelInQueueWorkItem() { Assert.ThrowsException <WorkItemCancelException>(() => { STPStartInfo stpStartInfo = new STPStartInfo { StartSuspended = true }; STP stp = new STP(stpStartInfo); IWorkItemResult wir = stp.QueueWorkItem(arg => null); wir.Cancel(); Assert.IsTrue(wir.IsCanceled); try { wir.GetResult(); } finally { stp.Shutdown(); } }); }
public void CancelInQueueWorkItem() { STPStartInfo stpStartInfo = new STPStartInfo(); stpStartInfo.StartSuspended = true; SmartThreadPool stp = new SmartThreadPool(stpStartInfo); IWorkItemResult wir = stp.QueueWorkItem(arg => null); wir.Cancel(); Assert.IsTrue(wir.IsCanceled); try { wir.GetResult(); } finally { stp.Shutdown(); } }
public void Abort() { if (this.Aborted) { return; } this.Aborted = true; this.abortRequest(); IWorkItemResult workItemResult = this.workItem; if (workItemResult != null) { workItemResult.Cancel(); } else { } this.workItem = null; if (!this.KeepEventsBound) { this.unbindEvents(); } }
/// <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); }
public bool Cancel() { return(_workItemResult.Cancel()); }
public bool Cancel() { return(wr.Cancel()); }
public bool Abort() { return(wr.Cancel(true)); }