/// <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(); // Timeout was reach? if (!m_threadPool.WaitForIdle(Timeout.TotalMilliseconds > int.MaxValue ? int.MaxValue : Convert.ToInt32(Timeout.TotalMilliseconds))) { if (m_threadPool.IsShuttingdown) { return(true); } else { m_threadPool.Cancel(true); 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(); IsRunning = false; } }
//停止扫描 public void stopScan() { stp.Cancel(); if (sThread != null) { sThread.Abort(); } while (stp.InUseThreads > 0) { Thread.Sleep(10); } this.Invoke(new voiddelegate(dostopScan)); }
public string Stop() //Strange bugs with exit.. update: still no idea { if (_running) { Logger.Log("MacFeeder stopped listening for Information", Logger.MessageType.INFORM); _running = false; _pool.Cancel(); _pool.Shutdown(); if (_check.ThreadState != ThreadState.Unstarted) { _check.Join(TimeSpan.FromSeconds(3)); } else { _check.Abort(); } if (_check.ThreadState == ThreadState.Running) { _check.Abort(); } if (_start.ThreadState != ThreadState.Unstarted) { _start.Join(TimeSpan.FromSeconds(3)); } else { _start.Abort(); } if (_start.ThreadState == ThreadState.Running) { _start.Abort(); } return(null); } else { Logger.Log("Tried to stopp MacFeeder, but there wasn't any running", Logger.MessageType.INFORM); return("Is already stopped"); } }
/// <summary> /// Method to cancel the working queue, see http://dotspatial.codeplex.com/discussions/473428 /// </summary> public void Clear() { _threadPool.Cancel(false); foreach (var request in _activeTileRequests.ToArray()) { int one; if (!_openTileRequests.ContainsKey(request.Key)) { if (!_activeTileRequests.TryRemove(request.Key, out one)) { _activeTileRequests.TryRemove(request.Key, out one); } } } _openTileRequests.Clear(); }
public void stopCraker() { if (stp != null && !stp.IsShuttingdown && this.crackerThread != null) { LogWarning("等待线程结束..."); stp.Cancel(); this.crackerThread.Abort(); 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 DropAllPendingTileRequests() { ThrowExceptionIfDisposed(); // Notes: http://dotspatial.codeplex.com/discussions/473428 threadPool.Cancel(false); foreach (KeyValuePair <TileIndex, int> request in activeTileRequests.ToArray()) { if (!openTileRequests.ContainsKey(request.Key)) { int dummy; if (!activeTileRequests.TryRemove(request.Key, out dummy)) { activeTileRequests.TryRemove(request.Key, out dummy); } } } openTileRequests.Clear(); }
public void Stop() { lock (_stateLock) { if (_state == 1) { _state = 2; _cycleThread.Join(_cycleTime + 100); if (_cycleThread.ThreadState != ThreadState.Stopped) { _cycleThread.Abort(); } _workerPool.WaitForIdle(_maxTimeToWaitForIdle); _workerPool.Cancel(); _state = 0; } } }
private void PingButton_OnClick_invoke() { // PingButton.IsEnabled = false; this.Dispatcher.Invoke(() => PingButton.IsEnabled = false); threadPool.Cancel(); // PingDataList.Clear(); this.Dispatcher.Invoke(() => PingDataList.Clear()); var select_str = this.Dispatcher.Invoke(() => ComboDomainBox.Text); var htmldata = HttpHelper.GetHtmlTask($"http://ping.chinaz.com/", select_str, new { host = select_str, linetype = "电信,多线,联通,移动,海外" }); if (String.IsNullOrWhiteSpace(htmldata.Encode)) { this.Dispatcher.Invoke(() => PingButton.IsEnabled = true); return; } var templist = new ObservableCollection <PingData>(); foreach (var guidLocalKey in htmldata.GuidLocal.Keys) { templist.Add(new PingData(guidLocalKey, htmldata.GuidLocal[guidLocalKey], select_str)); } this.Dispatcher.Invoke(() => { PingDataList = templist; PingDataGrid.ItemsSource = PingDataList; }); List <IWorkItemResult> threadResults = new List <IWorkItemResult>(); foreach (var guidLocalKey in htmldata.GuidLocal.Keys) { threadResults.Add(threadPool.QueueWorkItem(new WorkItemCallback(UpdateData), new UpdateDataParams(guidLocalKey, select_str, htmldata.Encode))); } SmartThreadPool.WaitAll(threadResults.ToArray()); Task.Factory.StartNew(LocalPing); this.Dispatcher.Invoke(() => PingButton.IsEnabled = true); // PingButton.IsEnabled = true; }
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(); }
private void btnCancel6_Click(object sender, EventArgs e) { _smartThreadPool.Cancel(); }
private static void stopthreadpool() { stp.Cancel(); }
public void Abort() { Pool.Cancel(true); }