/// <summary> /// <see cref=" Spectrometer"/> /// 当扣除背景选项选中后,扣除背景。测量过程先测背景,后测原始光谱。 /// </summary> /// <param name="spectrometer">Spectrometer handle</param> /// <param name="currentCircle">当前循环(Circles)次数</param> /// <returns>本次测量的数组,长度等于CCD横向(Horizontal)长度</returns> private double[] IsBackgroundSubtracted(Spectrometer spectrometer,int currentCircle) { double[] tempdark = new double[pixelHoriNum]; double[] tempSpec = new double[pixelHoriNum]; if (radioButtonSubtract.Checked&&radioButtonInSitu.Checked==false)//扣除背景 { spectrometer.laserEnabled = false; System.Threading.Monitor.Enter(this); tempdark = spectrometer.getSpectrum();//背景光测量 System.Threading.Monitor.Exit(this); spectrometer.laserEnabled = true; numericUpDownLaserPerc.Value = numericUpDownLaserPerc.Value; if(radioButtonPreflashOn.Checked&&radioButtonPreflashOff.Checked==false) IsPreflash(currentCircle); System.Threading.Monitor.Enter(this); tempSpec = spectrometer.getSpectrum(); System.Threading.Monitor.Exit(this); for (int i = 0; i < pixelHoriNum; i++) { tempSpec[i] -= tempdark[i]; } } else//不扣除背景 { spectrometer.laserEnabled = true; numericUpDownLaserPerc.Value = numericUpDownLaserPerc.Value; if (radioButtonPreflashOn.Checked && radioButtonPreflashOff.Checked == false) IsPreflash(currentCircle); tempSpec = spectrometer.getSpectrum(); } return tempSpec; }
//////////////////////////////////////////////////////////////////////// // Background Worker: Acquisition Threads //////////////////////////////////////////////////////////////////////// /// <summary> /// Perform all acquisitions in background threads so the GUI stays responsive. /// </summary> /// <remarks> /// Note that this method is used by potentially several different /// BackgroundWorkers in parallel (one per attached spectrometer). /// /// TODO: rename backgroundWorkerAcquisition /// </remarks> private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e) { Spectrometer spectrometer = (Spectrometer)e.Argument; SpectrometerState state = spectrometerStates[spectrometer]; string prefix = String.Format("Worker.{0}.{1}", spectrometer.model, spectrometer.serialNumber); state.running = true; BackgroundWorker worker = sender as BackgroundWorker; while (true) { // end thread if we've been asked to cancel if (worker.CancellationPending) { break; } // logger.debug("workerAcquisition: getting spectrum"); double[] raw = spectrometer.getSpectrum(); if (raw == null) { Thread.Sleep(100); continue; } // process for graphing lock (spectrometers) state.processSpectrum(raw); // end thread if we've completed our allocated acquisitions if (opts.scanCount > 0 && state.scanCount >= opts.scanCount) { break; } int delayMS = (int)Math.Max(100, opts.scanIntervalSec * 1000); if (delayMS > 0) { Thread.Sleep(delayMS); } } state.running = false; e.Result = spectrometer; // pass spectrometer handle to _Completed callback }
private void ContinousTask() { while (!cts.IsCancellationRequested) { Task task = new Task(new Action(() => { rawSpecData = s.getSpectrum(); }), cts.Token); task.ContinueWith(t => { if (cts.Token.IsCancellationRequested)//如果取消了,退出 return; else { chartRawSpec.BeginInvoke(new MethodInvoker(delegate { UpdateSpecGraph(); })); } }, TaskContinuationOptions.OnlyOnRanToCompletion); task.Start(); Thread.Sleep(10); } }
//////////////////////////////////////////////////////////////////////// // Background Worker //////////////////////////////////////////////////////////////////////// private void backgroundWorkerAcquisition_DoWork(object sender, DoWorkEventArgs e) { logger.log("[Acquisition] starting..."); if (spectrometer == null || !spectrometer.isOk()) { logger.display("Can't start acquisition (missing spectrometer or driver)"); return; } BackgroundWorker worker = sender as BackgroundWorker; int scanCount = 0; while (true) { double[] newRaw = spectrometer.getSpectrum(); if (newRaw == null || newRaw.Length != pixels) { logger.display("Error taking acquisition"); break; } // perform multi-scan averaging, if requested if (scansToAverage > 1) { for (int i = 1; i < scansToAverage; i++) { double[] tmpRaw = spectrometer.getSpectrum(); if (tmpRaw == null || tmpRaw.Length != pixels) { logger.display("Error taking acquisition"); break; } for (int j = 0; j < pixels; j++) { newRaw[j] += tmpRaw[j]; } } for (int i = 0; i < pixels; i++) { newRaw[i] = newRaw[i] / ((double)scansToAverage); } } // copy to graphable buffer Array.Copy(newRaw, rawSpectrum, pixels); // trigger graph update in GUI thread worker.ReportProgress(scanCount++); // necessary to ensure the GUI stays responsive int delay = integrationTimeMillisec; if (delay < 200) { delay = 200; } Thread.Sleep(delay); // end thread if we've been asked to cancel if (worker.CancellationPending) { logger.log("[Acquisition] closing"); e.Cancel = true; break; } } logger.log("[Acquisition] done"); }