private void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { if (Monitor.TryEnter(_timer)) { try { if (!_queue.Any()) { if (_threads.All(p => p == null || (p.ThreadState & ThreadState.Stopped) != 0)) { _timer.Stop(); _stopwatch.Stop(); CompletedQueueEvent?.Invoke(_stopwatch.Elapsed); IsBusy = false; } return; } for (int i = 0; i < _threads.Length; i++) { if (_threads[i] == null || (_threads[i].ThreadState & ThreadState.Stopped) != 0) { _threads[i] = new Thread(Do); _threads[i].Start(); } } } catch (Exception) { } finally { Monitor.Exit(_timer); } } }
/// <summary> /// 停止 /// </summary> public void Stop() { _timer.Stop(); foreach (var thread in _threads) { if (thread != null && thread.ThreadState == ThreadState.Running) { thread.Abort(); } } CompletedQueueEvent?.Invoke(_stopwatch.Elapsed); IsBusy = false; }