public void SavePassiveStim(int booth_number, StimulationTrain stim, uint microseconds_per_sample) { //Save the data to a file (or multiple files) List <StreamWriter> writers = this.GetStreamWriter(booth_number); //Log the current save time AFTER we get the stream (it is important that this happens AFTER and not before, because of //how we check for refreshing streams based on the date of the last stimulation that occurred). LastSaveForEachBooth[booth_number] = DateTime.Now; foreach (var w in writers) { if (w != null) { w.Write(stim.StimulationTime.ToString("yyyy-MM-dd:HH:mm:ss:ffffff")); w.Write(", " + microseconds_per_sample.ToString()); for (int j = 0; j < stim.Data.Count; j++) { w.Write(", "); w.Write(stim.Data[j].ToString("0.00")); } w.Write("\r\n"); //Flush the stream so that the data gets written to the file for sure. //If we don't flush the stream here, then the computer will decide when to flush it, which could be hours from now //And what if a power outage happened?!?!? The data would be lost. Flush the stream. w.Flush(); } } }
private void HandleStreaming(object sender, DoWorkEventArgs e) { Stopwatch refractory_period_timer = new Stopwatch(); //Set the oscilloscope's channel SetOscilloscopeChannel(); //Set up the trigger for this scope SetupTrigger(); //Loop as long as the background thread has not been cancelled while (!_backgroundWorker.CancellationPending) { if (State == ScopeState.Recording) { switch (TrialCollectionState) { case TrialState.SetupForNextTrigger: RunBlock(); TrialCollectionState = TrialState.WaitForTrigger; break; case TrialState.WaitForTrigger: bool r = CheckIfReady(); if (r) { TrialCollectionState = TrialState.FinalizingCollectionAfterTrigger; refractory_period_timer.Restart(); } break; case TrialState.FinalizingCollectionAfterTrigger: //Collect the most recent stimulation train var values = RetrieveOscilloscopeData(); //Create a StimulationTrain object to the store the data for the collected stim train StimulationTrain t = new StimulationTrain(); t.StimulationTime = DateTime.Now; t.Data = values.Select(d => MaxVoltage * (double)d / (double)this.ScopeMaxValue).ToList(); //Store the most recent stimulation train MostRecentStimulationTrain = t; //Notify that we have a new stimulation train BackgroundPropertyChanged("MostRecentStimulationTrain"); //Set the state to set up for a new stim train TrialCollectionState = TrialState.WaitingOnRefractoryPeriod; break; case TrialState.WaitingOnRefractoryPeriod: if (refractory_period_timer.ElapsedMilliseconds >= (RefractoryPeriod / 1000)) { TrialCollectionState = TrialState.SetupForNextTrigger; refractory_period_timer.Reset(); } break; } } //Notify the UI of any changes _backgroundWorker.ReportProgress(0); //Sleep the thread for a little while so we don't consume the CPU Thread.Sleep(33); } }