/// <summary> /// Builds a measurement /// </summary> /// <param name="PRT_m">The PRT being used in the measurement</param> /// <param name="MUX_m">The MUX type that the PRT is pluged into</param> /// <param name="bridge_m">The type of resistance bridge the PRT is plugged into</param> /// <param name="bridge_m">The channel for the measurement</param> /// <param name="bridge_m">A delegate to be called when temperature data becomes available</param> public TemperatureMeasurement(ref PRT PRT_m, ref MUX MUX_m, ref ResistanceBridge bridge_m, short channel, long measurement_index) { prt = PRT_m; mux = MUX_m; bridge = bridge_m; lab_location = ""; filename = ""; channel_for_measurement = channel; result = 0.0; measurement_index_ = measurement_index; //thread_count = measurement_index+1; x_data = new StringBuilder(""); y_data = new StringBuilder(""); date = DateTime.Now; assigned_thread_priority = 1; //make the fresh measurement added have the highest execution priority. //everytime we add a new measurement change the size of the thread array //Array.Resize(ref threads_running,(int) measurement_index); Array.Resize(ref current_measurements, (int)measurement_index + 1); current_measurements[measurement_index] = this; //Array.Resize(ref threadexecution, (int)measurement_index); //threadexecution[measurement_index] = true; }
public static void MeasureAll() { string init_string = String.Concat(TemperatureMeasurement.MeasurementList[0].bridge.GPIBSICL, TemperatureMeasurement.MeasurementList[0].bridge.GPIBAddr); ResistanceBridge.createInteropObject(); TemperatureMeasurement.MeasurementList[0].bridge.InitIO(init_string); //TemperatureMeasurement measuring = (TemperatureMeasurement)stateInfo; while (TemperatureMeasurement.MeasurementList.Length != 30) { TemperatureMeasurement.thread_running.Join(1000); } //The file paths string path; string path2; //create a file stream writer to put the data into System.IO.StreamWriter writer = null; //record the month we are in int month_ = System.DateTime.Now.Month; thread_running.Join(3000); while (true) { int i = 0; foreach (TemperatureMeasurement current_m in TemperatureMeasurement.MeasurementList) { TemperatureMeasurement.thread_running.Join(500); bool appenditure = false; path = TemperatureMeasurement.MeasurementList[i].directory + TemperatureMeasurement.MeasurementList[i].Filename + ".txt"; path2 = @"G:\Shared drives\MSL - Length\Length\Temperature Monitoring Data\" + TemperatureMeasurement.MeasurementList[i].Filename + "_" + System.DateTime.Now.Ticks.ToString() + ".txt"; try { //if the file exists append to it otherwise create a new file if (System.IO.File.Exists(path)) { appenditure = true; writer = System.IO.File.AppendText(path); } else { writer = System.IO.File.CreateText(path); } } catch (System.IO.IOException e) { //Caused because a file is already open for editing, solve by creating a new file and appending the date onto the filename writer = System.IO.File.CreateText(path2); } using (writer) { //if appending we don't need this again if (!appenditure) { writer.WriteLine("Automatically Generated File!\n"); } //set the current (incoming) measurements priority to be the lowest (biggest number) //measuring.AssignedThreadPriority = thread_count; //make the current thread wait until its priority reaches 1 //lock (lockthis) while (measuring.AssignedThreadPriority != 1) Monitor.Wait(lockthis); //---------------------------------------------------------------------START OF CRITICAL SECTION------------------------------------------------------------- //lock(lockthis) //{ //make sure the channel is correct (it may have been changed by another thread) TemperatureMeasurement.MeasurementList[i].setMUXChannel(); //sleep the thread for the specified dead time //Thread.Sleep((int)(measuring.Inverval * 1000)); //take the measurement double measurement_result = TemperatureMeasurement.MeasurementList[i].Measure(); TemperatureMeasurement.MeasurementList[i].Result = measurement_result; TemperatureMeasurement.MeasurementList[i].y_data.Append(measurement_result.ToString() + ","); //record the time of the measurement TemperatureMeasurement.MeasurementList[i].date_time = System.DateTime.Now; double ole_date = TemperatureMeasurement.MeasurementList[i].date_time.ToOADate(); TemperatureMeasurement.MeasurementList[i].x_data.Append(ole_date.ToString() + ","); //invoke the GUI to print the temperature data data(measurement_result , TemperatureMeasurement.MeasurementList[i].filename + " on CH" + TemperatureMeasurement.MeasurementList[i].channel_for_measurement.ToString() + " in " + TemperatureMeasurement.MeasurementList[i].lab_location + "\n" , i); TemperatureMeasurement.MeasurementList[i].Result = measurement_result; try { if ((measurement_result < 25.0) || (measurement_result > 15.0)) { measurement_anomalies++; } } catch (Exception) { continue; } try { //write the measurement to file writer.WriteLine(string.Concat(measurement_result.ToString() + ", " + TemperatureMeasurement.MeasurementList[i].MUX.getCurrentChannel().ToString() , "," + TemperatureMeasurement.MeasurementList[i].date_time.ToString() + ", " + TemperatureMeasurement.MeasurementList[i].lab_location , ", " + TemperatureMeasurement.MeasurementList[i].Filename)); writer.Flush(); } catch (System.IO.IOException) { MessageBox.Show("Issue writing to file - Check Drive"); writer.Close(); //writer = System.IO.File.CreateText("c:" + TemperatureMeasurement.MeasurementList[i].Filename); } //now that the critical section has finished let the exiting thread decrement all the measurement priorities //for (int i = 0; i < TemperatureMeasurement.ThreadCount; i++) //{ // current_measurements[i].AssignedThreadPriority--; // Monitor.PulseAll(lockthis); //} //if we have removed an item then we need to reorder the priorities //if (measuring.measurementRemoved) //{ // measuring.measurementRemoved = false; // for (long i = measuring.measurementRemovalIndex; i < TemperatureMeasurement.ThreadCount; i++) // { // current_measurements[i].AssignedThreadPriority--; // Monitor.PulseAll(lockthis); // } //} writer.Close(); } i++; } } //thread_count--; }