private async void ReadAndPlot() { bool viewChanged = false; bool getLock = false; try { _statusLock.Enter(ref getLock); if (_samplesPerView != _globalInfo.SamplesPerView) { RefreshRunTimeParameters(); viewChanged = true; } _globalInfo.AITask.ReadData(ref _aiData, _samplesPerView, _timeOut); _globalInfo.Status = TaskStatus.Running; } catch (Exception ex) { // 如果是超时错误则继续执行 if (ex.Message.Contains("TimeOut")) { return; } throw; } finally { if (getLock) { _statusLock.Exit(); } } // 不暂停状态下需要更新数据和函数缓存 if (_globalInfo.IsRunning) { Task transposeTask = null; transposeTask = Task.Run((Action)TransposeDataToCache); _globalInfo.SamplesInChart = _samplesPerView; _parentForm.Invoke(_globalInfo.WaveformPlot, this._aiData, _xStart, _xIncrement, _globalInfo.SamplesInChart); if (viewChanged) { _parentForm.Invoke(new Action(_parentForm.RefreshStatusLabel)); } await transposeTask; } }
private void TaskWork() { while (true) { try { ReadAndPlot(); } catch (Exception ex) { if (TaskRunning) { Stop(); _globalInfo.Status = TaskStatus.Error; _parentForm.Invoke(_showErrMethod, ex.Message, "Task Error"); } // not need // if (ex is ThreadAbortException) // { // break; // } } } }