public void WaitAllWithTimeoutFailure() { SmartThreadPool smartThreadPool = new SmartThreadPool(); IWorkItemResult [] wirs = new IWorkItemResult[5]; for (int i = 0; i < wirs.Length; ++i) { wirs[i] = smartThreadPool.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null); } bool timeout = !SmartThreadPool.WaitAll(wirs, 10, true); bool success = timeout; smartThreadPool.Shutdown(); Assert.IsTrue(success); }
public void DoWork() { SmartThreadPool smartThreadPool = new SmartThreadPool(); IWorkItemResult <double> wir = smartThreadPool.QueueWorkItem(new Func <double, double, double>(DoDiv), 10.0, 0.0); try { double result = wir.Result; } // Catch the exception that Result threw catch (WorkItemResultException e) { // Dump the inner exception which DoDiv threw Debug.WriteLine(e.InnerException); } smartThreadPool.Shutdown(); }
public void FuncT() { SmartThreadPool stp = new SmartThreadPool(); IWorkItemResult <int> wir = stp.QueueWorkItem(new Func <int, int>(f), 1); int y = wir.GetResult(); Assert.AreEqual(y, 2); try { wir.GetResult(); } finally { stp.Shutdown(); } }
public void ChainedDelegatesPostExecute() { Assert.Throws <NotSupportedException>(() => { SmartThreadPool stp = new SmartThreadPool(); PostExecuteWorkItemCallback postExecuteWorkItemCallback = DoPostExecute; postExecuteWorkItemCallback += DoPostExecute; stp.QueueWorkItem( new WorkItemCallback(DoWork), null, postExecuteWorkItemCallback); stp.WaitForIdle(); stp.Shutdown(); }); }
public void Timeout() { Assert.Throws <WorkItemTimeoutException>(() => { SmartThreadPool smartThreadPool = new SmartThreadPool(); IWorkItemResult wir = smartThreadPool.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null); try { wir.GetResult(500, true); } finally { smartThreadPool.Shutdown(); } }); }
public void BlockingCall() { SmartThreadPool smartThreadPool = new SmartThreadPool(); bool success = false; IWorkItemResult wir = smartThreadPool.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null); if (!wir.IsCompleted) { int result = (int)wir.GetResult(); success = (1 == result); } smartThreadPool.Shutdown(); Assert.IsTrue(success); }
protected void Stop() { lock (m_SyncRoot) { if (m_Disposed) { throw new ObjectDisposedException(GetType().FullName); } if (!m_Started) { throw new InvalidOperationException("SmartThreadPool not started."); } SmartThreadPool.Shutdown(); m_Started = false; } }
public static void UpdatePrice() { var list = WayBillController.GetUpdatePriceWayBillList(); if (list == null || list.Count <= 0) { return; } if (ismultithreading.ToLowerInvariant() == "no") { //单线程 foreach (var model in list) { if (!WayBillController.UpdatePriceWayBill(FreightController.GetFreightPrice(model), model.WayBillNumber, model.ReceivingExpenseID)) { Log.Error(string.Format("运单号:{0}更新错误!", model.WayBillNumber)); } } } else if (ismultithreading.ToLowerInvariant() == "yes") { //多线程 if (minThreads > maxThreads) { maxThreads = minThreads; } var threadPool = new SmartThreadPool { MaxThreads = maxThreads < 1 ? 1 : maxThreads, MinThreads = minThreads < 1 ? 1 : minThreads }; var pendingWorkItems = new IWorkItemResult[list.Count]; for (int i = 0; i < list.Count; i++) { pendingWorkItems[i] = threadPool.QueueWorkItem(new WorkItemCallback(MUpdatePrice), list[i]); } if (SmartThreadPool.WaitAll(pendingWorkItems)) { threadPool.Shutdown(); } } }
public void CancelInQueueWorkItem() { Assert.Throws <WorkItemCancelException>(() => { STPStartInfo stpStartInfo = new STPStartInfo(); stpStartInfo.StartSuspended = true; bool hasRun = false; SmartThreadPool stp = new SmartThreadPool(stpStartInfo); IWorkItemResult wir = stp.QueueWorkItem( new WorkItemInfo() { Timeout = 500 }, state => { hasRun = true; return(null); }); Assert.IsFalse(wir.IsCanceled); Thread.Sleep(2000); Assert.IsTrue(wir.IsCanceled); stp.Start(); stp.WaitForIdle(); Assert.IsFalse(hasRun); try { wir.GetResult(); } finally { stp.Shutdown(); } }); }
/// <summary> /// Starts the tasks execution. /// </summary> /// <returns>If has reach the timeout false, otherwise true.</returns> public override bool Start() { base.Start(); m_threadPool = new SmartThreadPool(); try { m_threadPool.MinThreads = MinThreads; m_threadPool.MaxThreads = MaxThreads; var workItemResults = new IWorkItemResult[Tasks.Count]; for (int i = 0; i < Tasks.Count; i++) { var t = Tasks[i]; workItemResults[i] = m_threadPool.QueueWorkItem(new WorkItemCallback(Run), t); } m_threadPool.Start(); if (!m_threadPool.WaitForIdle(Timeout.TotalMilliseconds > int.MaxValue ? int.MaxValue : Convert.ToInt32(Timeout.TotalMilliseconds))) { return(false); } foreach (var wi in workItemResults) { Exception ex; wi.GetResult(out ex); if (ex != null) { throw ex; } } return(true); } finally { m_threadPool.Shutdown(true, 1000); m_threadPool.Dispose(); } }
public void ChainedDelegatesPostExecute() { SmartThreadPool smartThreadPool = new SmartThreadPool(); IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(int.MaxValue); PostExecuteWorkItemCallback postExecuteWorkItemCallback = new PostExecuteWorkItemCallback(DoPostExecute); postExecuteWorkItemCallback += new PostExecuteWorkItemCallback(DoPostExecute); workItemsGroup.QueueWorkItem( new WorkItemCallback(DoWork), null, postExecuteWorkItemCallback); workItemsGroup.WaitForIdle(); smartThreadPool.Shutdown(); }
private void RunUpdateFeedPool() { results.Clear(); feedPool = SetupSmartThreadPool(settings.UpdateFeedPoolThreads); //Add Items addFeedsToQueue(); feedPool.Start(); SmartThreadPool.WaitAll(results.ToArray()); feedList.Clear(); foreach (var r in results) { feedList.Add((InnerTubeFeed)r.Result); } feedPool.Shutdown(); WriteStatus("Update Feeds complete"); }
public void TestChoice() { SmartThreadPool stp = new SmartThreadPool(); int index = stp.Choice( () => Thread.Sleep(1000), () => Thread.Sleep(1500), () => Thread.Sleep(500)); Assert.AreEqual(2, index); index = stp.Choice( () => Thread.Sleep(300), () => Thread.Sleep(100), () => Thread.Sleep(200)); Assert.AreEqual(1, index); stp.Shutdown(); }
public void WaitAllWithTimeoutSuccess() { SmartThreadPool smartThreadPool = new SmartThreadPool(); IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(int.MaxValue); IWorkItemResult [] wirs = new IWorkItemResult[5]; for (int i = 0; i < wirs.Length; ++i) { wirs[i] = workItemsGroup.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null); } bool timeout = !SmartThreadPool.WaitAll(wirs, 1500, true); bool success = !timeout; smartThreadPool.Shutdown(); Assert.IsTrue(success); }
public void TestThreadsEvents() { ClearResults(); SmartThreadPool stp = new SmartThreadPool(1000); stp.OnThreadInitialization += OnInitialization; stp.OnThreadTermination += OnTermination; stp.QueueWorkItem(new WorkItemCallback(DoSomeWork), null); stp.WaitForIdle(); // .net core Thread.Abort is not supported, so wait for the thread to go idle and self terminate stp.Shutdown(Timeout.Infinite); Assert.IsTrue(_initSuccess); Assert.IsTrue(_workItemSuccess); Assert.IsTrue(_termSuccess); }
public void stopCraker() { if (stp != null && !stp.IsShuttingdown && this.crackerThread != null) { LogWarning("等待线程结束..."); this.crackerThread.Abort(); stp.Shutdown(); while (stp.InUseThreads > 0) { Thread.Sleep(50); } //更新状态 this.Invoke(new update(updateStatus)); this.btn_cracker.Enabled = true; this.services_list.Enabled = true; this.bt_timer.Stop(); LogWarning("全部线程已停止!"); } }
public void TestWIGConcurrencyChange1WIG() { SmartThreadPool smartThreadPool = new SmartThreadPool(10 * 1000, 1, 0); Assert.IsTrue(0 == smartThreadPool.WaitingCallbacks); PauseSTP(smartThreadPool); Thread.Sleep(100); Assert.IsTrue(0 == smartThreadPool.WaitingCallbacks); IWorkItemsGroup wig = smartThreadPool.CreateWorkItemsGroup(1); wig.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null); Assert.IsTrue(1 == smartThreadPool.WaitingCallbacks); wig.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null); Assert.IsTrue(1 == smartThreadPool.WaitingCallbacks); wig.Concurrency = 2; Thread.Sleep(100); Assert.IsTrue(2 == smartThreadPool.WaitingCallbacks); ResumeSTP(smartThreadPool); Thread.Sleep(100); Assert.IsTrue(0 == smartThreadPool.WaitingCallbacks); PauseSTP(smartThreadPool); wig.Concurrency = 1; wig.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null); Assert.IsTrue(1 == smartThreadPool.WaitingCallbacks); wig.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null); Assert.IsTrue(1 == smartThreadPool.WaitingCallbacks); ResumeSTP(smartThreadPool); Thread.Sleep(100); Assert.IsTrue(0 == smartThreadPool.WaitingCallbacks); smartThreadPool.Shutdown(); }
//[ExpectedException(typeof(WorkItemCancelException))] public void CancelCancelledWorkItemAbort() { Assert.Throws <WorkItemCancelException>(() => { 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 WaitForIdle() { SmartThreadPool smartThreadPool = new SmartThreadPool(10 * 1000, 25, 0); ManualResetEvent isRunning = new ManualResetEvent(false); for (int i = 0; i < 4; ++i) { smartThreadPool.QueueWorkItem(delegate { isRunning.WaitOne(); }); } bool success = !smartThreadPool.WaitForIdle(1000); isRunning.Set(); success = success && smartThreadPool.WaitForIdle(1000); smartThreadPool.Shutdown(); Assert.IsTrue(success); }
public void TimeoutCompletedWorkItem() { SmartThreadPool stp = new SmartThreadPool(); IWorkItemResult wir = stp.QueueWorkItem( new WorkItemInfo() { Timeout = 500 }, state => 1); stp.WaitForIdle(); Assert.AreEqual(wir.GetResult(), 1); Thread.Sleep(1000); Assert.AreEqual(wir.GetResult(), 1); stp.Shutdown(); }
public void WaitAnyWithTimeoutFailure() { SmartThreadPool smartThreadPool = new SmartThreadPool(); IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(int.MaxValue); IWorkItemResult [] wirs = new IWorkItemResult[5]; for (int i = 0; i < wirs.Length; ++i) { wirs[i] = workItemsGroup.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null); } int index = SmartThreadPool.WaitAny(wirs, 10, true); bool success = (index == WaitHandle.WaitTimeout); smartThreadPool.Shutdown(); Assert.IsTrue(success); }
public static void TestMultipleSerialization() { //MessageSharkSerializer.Build(); var count = 10000; SmartThreadPool smartThreadPool = new SmartThreadPool(); IWorkItemResult[] tasks = new IWorkItemResult[count]; for (var i = 0; i < count; i++) { SmartThreadPool st = new SmartThreadPool(); tasks[i] = st.QueueWorkItem(() => { var data = new Message() { ID = 10, CreateDate = DateTime.Now, Data = "This is a test" }; var buffer666 = MessageSharkSerializer.Serialize(data); //threads.Add(Thread.CurrentThread.ManagedThreadId); }); } bool success = SmartThreadPool.WaitAll(tasks); //if (success) result1 = (int)wir1.Result; smartThreadPool.Shutdown(); //var tasks = new Task[count]; ////var threads = new ConcurrentBag<int>(); //for (var i = 0; i < count; i++) //{ // tasks[i] = Task.Factory.StartNew(() => // { // var data = new Message() { ID = 10, CreateDate = DateTime.Now, Data = "This is a test" }; // var buffer = MessageSharkSerializer.Serialize(data); // //threads.Add(Thread.CurrentThread.ManagedThreadId); // }).ContinueWith(t => Handle(t), TaskContinuationOptions.OnlyOnFaulted) // .ContinueWith(_ => { }); //} //Task.WaitAll(tasks); }
public void WaitAnyWithTimeoutSuccess() { SmartThreadPool smartThreadPool = new SmartThreadPool(); bool success = true; IWorkItemResult [] wirs = new IWorkItemResult[5]; for (int i = 0; i < wirs.Length; ++i) { wirs[i] = smartThreadPool.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null); } int index = SmartThreadPool.WaitAny(wirs, 1500, true); success = (index != WaitHandle.WaitTimeout); smartThreadPool.Shutdown(); Assert.IsTrue(success); }
public void DoWork() { SmartThreadPool smartThreadPool = new SmartThreadPool(); IWorkItemResult wir1 = smartThreadPool.QueueWorkItem(new WorkItemCallback(this.DoSomeWork1), null); IWorkItemResult wir2 = smartThreadPool.QueueWorkItem(new WorkItemCallback(this.DoSomeWork2), null); bool success = SmartThreadPool.WaitAll(new IWorkItemResult [] { wir1, wir2 }); if (success) { int result1 = (int)wir1.Result; int result2 = (int)wir2.Result; } smartThreadPool.Shutdown(); }
public void WaitAll() { SmartThreadPool smartThreadPool = new SmartThreadPool(); IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(int.MaxValue); bool success = true; IWorkItemResult [] wirs = new IWorkItemResult[5]; for (int i = 0; i < wirs.Length; ++i) { wirs[i] = workItemsGroup.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null); } SmartThreadPool.WaitAll(wirs); for (int i = 0; i < wirs.Length; ++i) { if (!wirs[i].IsCompleted) { success = false; break; } else { int result = (int)wirs[i].GetResult(); if (1 != result) { success = false; break; } } } smartThreadPool.Shutdown(); Assert.IsTrue(success); }
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 DoWork(object [] states) { STPStartInfo stpStartInfo = new STPStartInfo(); stpStartInfo.StartSuspended = true; SmartThreadPool smartThreadPool = new SmartThreadPool(stpStartInfo); foreach (object state in states) { smartThreadPool.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), state); } // Start working on the work items in the queue smartThreadPool.Start(); // Wait for the completion of all work items smartThreadPool.WaitForIdle(); smartThreadPool.Shutdown(); }
public void CancelCanceledWorkItem() { Assert.Throws <WorkItemCancelException>(() => { 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 Timeout() { SmartThreadPool smartThreadPool = new SmartThreadPool(); IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(int.MaxValue); bool success = false; IWorkItemResult wir = workItemsGroup.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null); try { wir.GetResult(500, true); } catch (WorkItemTimeoutException) { success = true; } smartThreadPool.Shutdown(); Assert.IsTrue(success); }
public void CancelSTPWorkItems() { // 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(); for (int i = 0; i < 10; i++) { stp.QueueWorkItem( state => { Thread.Sleep(500); ++counter; return(null); } ); } Thread.Sleep(100); stp.Cancel(true); Assert.AreEqual(counter, 0); stp.Shutdown(); }