public void StopMonitorFolder() { if (_fSWatcher != null) { _fSWatcher.EnableRaisingEvents = false; _fSWatcher.Created -= new FileSystemEventHandler(FSWatcher_Created); _fSWatcher = null; this.FMSObjStatus = "Stop"; } if ((_processFileWIG != null)) { _processFileWIG.Cancel(true); _processFileWIG = null; } if (_sMTPool != null) { _sMTPool.Shutdown(); _sMTPool = null; } if ((_processingFileList != null)) { _processingFileList = null; } }
private void QueueWorkItems(IWorkItemsGroup group, IEnumerable <Action> actionGroup) { foreach (Action action in actionGroup) { Action action_ = action; group.QueueWorkItem( o => { try { action_(); } catch (Exception e) { ExecutionException = e; bool abort = false; if (OnException != null) { OnException(e, out abort); } group.Cancel(abort); } return(null); }, this, wi => { //if (wi.Exception != null) { } }); } }
public void WaitForIdleWithCancel() { SmartThreadPool smartThreadPool = new SmartThreadPool(10 * 1000, 1, 1); IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(2); _x = 0; IWorkItemResult wir1 = workItemsGroup.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), 1000); IWorkItemResult wir2 = workItemsGroup.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), 1000); IWorkItemResult wir3 = workItemsGroup.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), 1000); while (0 == _x) { Thread.Sleep(10); } //Console.WriteLine("{0}: Cancelling WIG", DateTime.Now.ToLongTimeString()); workItemsGroup.Cancel(); // At this point: // The first work item is running // The second work item is cancelled, but waits in the STP queue // The third work item is cancelled. Assert.AreEqual(1, _x); Assert.IsTrue(wir2.IsCanceled); Assert.IsTrue(wir3.IsCanceled); // Make sure the workItemsGroup is still idle Assert.IsFalse(workItemsGroup.IsIdle); //Console.WriteLine("{0}: Waiting for 1st result", DateTime.Now.ToLongTimeString()); wir1.GetResult(); Assert.AreEqual(2, _x); bool isIdle = workItemsGroup.WaitForIdle(100); Assert.IsTrue(isIdle); smartThreadPool.Shutdown(); }
public void stop(Boolean abort) { if (this.RunStatus != Constant.RunStatus.STOP) { lock (_lock) { if (this.RunStatus != Constant.RunStatus.STOP) { this.RunStatus = Constant.RunStatus.STOP; this.waitQueue = null; this.startQueue = null; this.successQueueCnt = successQueue.Count; this.successQueue = null; this.errorQueueCnt = errorQueue.Count; this.errorQueue = null; IWorkItemsGroup oldWorkItemsGroup = this.workItemsGroup; this.workItemsGroup = null; Thread mailThread = new Thread(new ThreadStart(delegate { try { oldWorkItemsGroup.Cancel(false); oldWorkItemsGroup = null; } catch { } })); mailThread.Name = "stop workItemsGroup thread"; mailThread.IsBackground = false; mailThread.Priority = ThreadPriority.Normal; mailThread.Start(); } } } }
public void Cancel1WIGof2WorkItems() { int counter1 = 0; int counter2 = 0; SmartThreadPool stp = new SmartThreadPool(); IWorkItemsGroup wig1 = stp.CreateWorkItemsGroup(3); IWorkItemsGroup wig2 = stp.CreateWorkItemsGroup(3); for (int i = 0; i < 3; i++) { wig1.QueueWorkItem( state => { Interlocked.Increment(ref counter1); Thread.Sleep(500); Interlocked.Increment(ref counter1); return(null); } ); } for (int i = 0; i < 3; i++) { wig2.QueueWorkItem( state => { Thread.Sleep(500); Interlocked.Increment(ref counter2); return(null); } ); } while (counter1 < 3) { Thread.Sleep(1); } wig1.Cancel(true); stp.WaitForIdle(); Assert.AreEqual(3, counter1, "Cancelled WIG1"); Assert.AreEqual(3, counter2, "Normal WIG2"); stp.Shutdown(); }
public void CancelWIGWorkItems() { // I don't use lock on the counter, since any number above 0 is a failure. // In the worst case counter will be equal to 1 which is still not 0. int counter = 0; SmartThreadPool stp = new SmartThreadPool(); IWorkItemsGroup wig = stp.CreateWorkItemsGroup(10); for (int i = 0; i < 10; i++) { wig.QueueWorkItem( state => { Thread.Sleep(500); ++counter; return(null); } ); } Thread.Sleep(100); wig.Cancel(true); Assert.AreEqual(counter, 0); stp.Shutdown(); }
private void btnCancel3_Click(object sender, EventArgs e) { _wig3.Cancel(); }