// This function reads and displays the temperature inputs by detecting
        // the buttons clicked, by detecting the value of the variable "check".
        // It runs a while loop to keep checking for button presses, and using
        // Thread.Sleep(), it checks buttons, reads and displays temperatures in
        // the selected scale at a set frequency (2 Hz). The loop ends when the
        // value of check is set to -1 by clicking the Exit button. Temperatures
        // are read using the TInScan function, and an array is passed to the
        // update_temp() function to display them using a delegate.
        private void temp_reader()
        {
            while (read_check != -1)
               {
                int count = 0;
                float[] TempData = new float[MAX_CHANCOUNT];
                float[] TempData_C = new float[MAX_CHANCOUNT];

                MccBoard daq = new MccDaq.MccBoard(BoardNum);
                MccDaq.ErrorInfo RetVal;

                Update_Temperature update_temp_del = new
                    Update_Temperature(update_temp);

                Update_Warning update_warning_del = new
                    Update_Warning(update_warning);

                Reset_Checkboxes resetcheckboxes_del = new
                    Reset_Checkboxes(resetcheckboxes);

                for (int i = 0; i < MAX_CHANCOUNT; i++)
                {
                    if (((CheckBox)this.Controls["Ch" +
                        i.ToString("#0") + "Box"]).Enabled == true &&
                        ((CheckBox)this.Controls["Ch" +
                        i.ToString("#0") + "Box"]).Checked == true)
                    {
                        count++;
                    }
                }

                if (count == 0)
                {
                    TempData = new float[MAX_CHANCOUNT] {-9999, -9999,
                        -9999, -9999, -9999, -9999, -9999, -9999};

                    Array.Copy(TempData, TempData_C, 8);

                    not_showing_temp = true;
                    Invoke(update_temp_del, TempData, TempData_C);
                }
                else
                    not_showing_temp = false;

                if (read_check == 1 && not_showing_temp == false)
                {
                    for (int i = 0; i < MAX_CHANCOUNT; i++)
                    {
                        if (((CheckBox)this.Controls["Ch" +
                            i.ToString("#0") + "Box"]).Checked == true)
                        {
                            RetVal = daq.TIn(i, TempScale.Celsius,
                                out TempData_C[i],
                                ThermocoupleOptions.Filter);
                            IsError(this,RetVal, true);
                        }
                        else
                            TempData_C[i] = -9999;
                    }

                    switch(my_TempScale)
                    {
                        case TempScale.Fahrenheit:
                            for (int i = 0; i < MAX_CHANCOUNT; i++)
                            {
                                if (((CheckBox)this.Controls["Ch" +
                                    i.ToString("#0") + "Box"]).Checked == true)
                                {
                                    RetVal = daq.TIn(i, TempScale.Fahrenheit,
                                        out TempData[i],
                                        ThermocoupleOptions.Filter);
                                    IsError(this,RetVal, true);
                                }
                                else
                                    TempData[i] = -9999;
                            }
                            break;

                        case TempScale.Kelvin:
                            for (int i = 0; i < MAX_CHANCOUNT; i++)
                            {
                                if (((CheckBox)this.Controls["Ch" +
                                    i.ToString("#0") + "Box"]).Checked == true)
                                {
                                    RetVal = daq.TIn(i, TempScale.Kelvin,
                                        out TempData[i],
                                        ThermocoupleOptions.Filter);
                                    IsError(this,RetVal, true);
                                }
                                else
                                    TempData[i] = -9999;
                            }
                            break;

                        default:
                            for (int i = 0; i < MAX_CHANCOUNT; i++)
                            {
                                if (((CheckBox)this.Controls["Ch" +
                                    i.ToString("#0") + "Box"]).Checked == true)
                                {
                                    RetVal = daq.TIn(i, TempScale.Celsius,
                                        out TempData[i],
                                        ThermocoupleOptions.Filter);
                                    IsError(this,RetVal, true);
                                }
                                else
                                    TempData[i] = -9999;
                            }
                            break;
                    }

                    Invoke(update_temp_del, TempData, TempData_C);

                    Thread.Sleep(500);
                }

                if ((read_check == 0 && oven_check == 1) ||
                    (not_showing_temp == true && oven_check == 1))
                {
                    string temp = String.Format("WARNING! Temperature" +
                        " readings are no longer live during an oven run!\n" +
                        "Automatically fixing this in {0} seconds...",
                        (int)(warning_time/2));

                    Invoke(update_warning_del, temp, 1);

                    Thread.Sleep(500);

                    warning_time--;

                    if (warning_time <= 0)
                    {
                        read_check = 1;
                        Invoke(resetcheckboxes_del);
                        not_showing_temp = false;
                        Invoke(update_warning_del, " ", 0);
                        warning_time = 20;
                    }
                }

                // Make sure that the text is grayed out when the Stop TC button
                // is in its clicked state.
                if (read_check == 0 && oven_check != 1)
                {
                    TempData[0] = 1234;
                    TempData_C[0] = 1234;
                    Invoke(update_temp_del, TempData, TempData_C);
                    Thread.Sleep(500);
                }
            }
        }
        // This function reads and displays the temperature inputs by detecting
        // the buttons clicked, by detecting the value of the variable "check".
        // It runs a while loop to keep checking for button presses, and using
        // Thread.Sleep(), it checks buttons, reads and displays temperatures in
        // the selected scale at a set frequency (2 Hz). The loop ends when the
        // value of check is set to -1 by clicking the Exit button. Temperatures
        // are read using the TInScan function, and an array is passed to the
        // update_temp() function to display them using a delegate.
        private void ReadTemperature()
        {
            while (read_check != ComponentCheckStatusEnum.Aborted)
            {
                int count = 0;
                float[] TempData = new float[MAX_CHANCOUNT];
                float[] TempData_C = new float[MAX_CHANCOUNT];

                MccDaq.ErrorInfo RetVal;

                Update_Temperature update_temp_del = new
                    Update_Temperature(UpdateTemperatureDisplays);

                Update_Warning update_warning_del = new
                    Update_Warning(UpdateWarningDisplay);

                Reset_Checkboxes resetcheckboxes_del = new
                    Reset_Checkboxes(ResetThemocoupleInputStatusCheckboxes);

                for (int i = 0; i < MAX_CHANCOUNT; i++)
                {
                    if (((CheckBox)this.Controls["Channel" +
                        i.ToString("#0") + "CheckBox"]).Enabled == true &&
                        ((CheckBox)this.Controls["Channel" +
                        i.ToString("#0") + "CheckBox"]).Checked == true)
                    {
                        count++;
                    }
                }

                if (count == 0)
                {
                    TempData = new float[MAX_CHANCOUNT] {-9999, -9999,
                        -9999, -9999, -9999, -9999, -9999, -9999};

                    //Array.Copy(TempData, TempData_C, 8);

                    not_showing_temp = true;
                    //BeginInvoke(update_temp_del, TempData, TempData_C);
                }
                else
                    not_showing_temp = false;

                if (read_check == ComponentCheckStatusEnum.Running &&
                    not_showing_temp == false)
                {
                    for (int i = 0; i < MAX_CHANCOUNT; i++)
                    {
                        if (((CheckBox)this.Controls["Channel" +
                            i.ToString("#0") + "CheckBox"]).Checked == true) // null object reference error, suggests using "new" keyword
                        {
                            RetVal = thermocouple_board.TIn(i, TempScale.Celsius,
                                out TempData_C[i],
                                ThermocoupleOptions.Filter);
                            IsError(this, RetVal, true);
                        }
                        else
                            TempData_C[i] = -9999;

                        while (IsLockedCurrentTemp_Array)
                        {
                            //Nothing - pause till lock lifted
                        }

                        CurrentTemperatures[i] = TempData_C[i];

                        if (PlateauTemperatureWindow == null)
                            PlateauTemperatureWindow = new List<float[]>();

                        AddToMaxTemperaturePlateau(ref TempData_C);
                        MaxTempPlateauReached = IsAtSetTemperaturePlateau();
                    }

                    switch (my_TempScale)
                    {
                        case TempScale.Fahrenheit:
                            for (int i = 0; i < MAX_CHANCOUNT; i++)
                            {
                                if (((CheckBox)this.Controls["Channel" +
                                    i.ToString("#0") + "CheckBox"]).Checked == true)
                                {
                                    RetVal = thermocouple_board.TIn(i, TempScale.Fahrenheit,
                                        out TempData[i],
                                        ThermocoupleOptions.Filter);
                                    IsError(this, RetVal, true);
                                }
                                else
                                    TempData[i] = -9999;
                            }
                            break;

                        case TempScale.Kelvin:
                            for (int i = 0; i < MAX_CHANCOUNT; i++)
                            {
                                if (((CheckBox)this.Controls["Channel" +
                                    i.ToString("#0") + "CheckBox"]).Checked == true)
                                {
                                    RetVal = thermocouple_board.TIn(i, TempScale.Kelvin,
                                        out TempData[i],
                                        ThermocoupleOptions.Filter);
                                    IsError(this, RetVal, true);
                                }
                                else
                                    TempData[i] = -9999;
                            }
                            break;

                        default:
                            for (int i = 0; i < MAX_CHANCOUNT; i++)
                            {
                                if (((CheckBox)this.Controls["Channel" +
                                    i.ToString("#0") + "CheckBox"]).Checked == true)
                                {
                                    RetVal = thermocouple_board.TIn(i, TempScale.Celsius,
                                        out TempData[i],
                                        ThermocoupleOptions.Filter);
                                    IsError(this, RetVal, true);
                                }
                                else
                                    TempData[i] = -9999;
                            }
                            break;
                    }

                    BeginInvoke(update_temp_del, TempData, TempData_C);
                    System.Threading.Thread.Sleep(500);
                }

                if ((read_check == ComponentCheckStatusEnum.None &&
                     oven_check == ComponentCheckStatusEnum.Running) ||
                    (not_showing_temp == true &&
                     oven_check == ComponentCheckStatusEnum.Running))
                {
                    string temp =
                        String.Format(
                            "WARNING! Temperature readings are no longer live during an oven run!{0}" +
                            "Automatically fixing this in {1} seconds...",
                            Environment.NewLine,
                            ((int)(warning_time / 2)).ToString("0"));

                    BeginInvoke(update_warning_del, temp, 1);

                    Thread.Sleep(500);

                    warning_time--;

                    if (warning_time <= 0)
                    {
                        read_check = ComponentCheckStatusEnum.Running;
                        BeginInvoke(resetcheckboxes_del);
                        not_showing_temp = false;
                        BeginInvoke(update_warning_del, " ", 0);
                        warning_time = 20;
                    }
                }

                // Make sure that the text is grayed out when the Stop TC button
                // is in its clicked state.
                if (read_check == ComponentCheckStatusEnum.None &&
                    oven_check != ComponentCheckStatusEnum.Running)
                {
                    TempData[0] = 1234;
                    TempData_C[0] = 1234;
                    BeginInvoke(update_temp_del, TempData, TempData_C);
                    Thread.Sleep(500);
                }
            }
        }