private bool disposedValue = false; // To detect redundant calls

        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    // TODO: dispose managed state (managed objects).
                }

                // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.

                StopTempMonitor();

                _deviceSlider       = null;
                _deviceR1Piston     = null;
                _deviceR2Piston     = null;
                _deviceHeaterPiston = null;
                _devicePCRCycler    = null;
                _devicePump         = null;
                _deviceOpticsMotor  = null;
                _deviceHeater       = null;

                _opticsBoard = null;
                _motorBoard  = null;
                _motorBoardX = null;
                _tecBoard.theBoard.Dispose();
                _tecBoard = null;

                disposedValue = true;
            }
        }
        //theModel.m_data_r1.Add(temp_data);
        //for (uint ui = 5; ui <theModel.m_data_t.Count-5 ; ui++)
        //{

        //}

        public async Task <bool> Initialize()
        {
            bool bInitialized = true;

            logger.Debug("----- Starting -----");

            SendProgress(0, "Initializing");

            // Kickoff a process to read the configuration.

            //_state = instrumentState.isSettingUp;

            CheckFileTimeStamps();

            Task <bool> configLoadResult = _config.Load(CSystem_Defns.strDefaultSystemConfigurationPath);

            SendProgress(0.1f, "Loading system settings.");

            // Await the completion of loading config.

            bool bResult = await configLoadResult;

            // Do a quick check to see that all the COM ports are there, and if not,
            // suggest to the user that maybe the instrument is not turned on.

            List <string> comPortLists = new List <string>();

            comPortLists.Add(Config.m_MotorControllerConfigurations["MC-1"].m_strPort);
            comPortLists.Add(Config.m_MotorControllerConfigurations["MC-2"].m_strPort);
            comPortLists.Add(Config.m_ThermalControllerConfigurations["ACCEL-TEC-CNTRL-1"].m_strPort);
            //comPortLists.Add(Config.m_OpticsController_Configuration.m_strPort);//2018

            bool bGoodPort = false;

            while (!bGoodPort)
            {
                foreach (string sPort in comPortLists)
                {
                    SerialPort stuntPort = new SerialPort(sPort);
                    bGoodPort = true;

                    try
                    {
                        stuntPort.Open();
                        stuntPort.Close();
                    }
                    catch (Exception)
                    {
                        bGoodPort = false;
                    }

                    if (!bGoodPort)
                    {
                        break;
                    }
                }

                if (!bGoodPort)
                {
                    MessageBox.Show("Not all ports are present in the system.  Is the instrument turned on?",
                                    "Can't find all ports");
                    break;
                }
            }

            // Things that require the configuration must be after this line.

            // Initialize the optics board.

            // _opticsBoard.Initialize();
            SendProgress(0.2f, "the Optics Subsystem.");

            List <Task> initTasks = new List <Task>();

            // Initialize the motor controller.


            Task <bool> bMbInit = _motorBoard.Initialize(Config.m_MotorControllerConfigurations["MC-1"].m_strPort);

            initTasks.Add(bMbInit);

            SendProgress(0.3f, "Initializing the first Motor Controller.");

            // Initialize the second motor controller.

            Task <bool> bMbXInit = _motorBoardX.Initialize(Config.m_MotorControllerConfigurations["MC-2"].m_strPort);

            initTasks.Add(bMbXInit);

            SendProgress(0.4f, "Initializing the second Motor Controller.");

            // Initialize the temp controller.

            Task <bool> bTbInit = _tecBoard.Initialize();

            initTasks.Add(bTbInit);

            SendProgress(0.5f, "Initializing the Temperature controller.");

            // Wait for all the boards to init.

            await Task.WhenAll(initTasks);

            motorControllers["MC-1"] = _motorBoard;
            motorControllers["MC-2"] = _motorBoardX;

            // Construct the devices.

            _deviceSlider = new deviceSlider(motorControllers[_config.m_Slider_Configuration.m_strControllerName],
                                             _config.m_Slider_Configuration.m_nMotorChannel,
                                             _config.m_Slider_Configuration.positions,
                                             _config.m_Slider_Configuration.m_strControllerName);
            _deviceR2Piston = new deviceR2Piston(motorControllers[_config.m_Slider_Configuration.m_strControllerName],
                                                 _config.m_R2Piston_Configuration.m_nMotorChannel,
                                                 _config.m_R2Piston_Configuration.positions,
                                                 _config.m_R2Piston_Configuration.m_strControllerName);
            _deviceR1Piston = new deviceR1Piston(motorControllers[_config.m_R1Piston_Configuration.m_strControllerName],
                                                 _config.m_R1Piston_Configuration.m_nMotorChannel,
                                                 _config.m_R1Piston_Configuration.positions,
                                                 _config.m_R1Piston_Configuration.m_strControllerName);
            _deviceHeaterPiston = new deviceHeaterPiston(motorControllers[_config.m_HeaterPiston_Configuration.m_strControllerName],
                                                         _config.m_HeaterPiston_Configuration.m_nMotorChannel,
                                                         _config.m_HeaterPiston_Configuration.positions,
                                                         _config.m_HeaterPiston_Configuration.m_strControllerName);
            _deviceChassisPiston = new deviceChassisPiston(motorControllers[_config.m_ChassisPiston_Configuration.m_strControllerName],
                                                           _config.m_ChassisPiston_Configuration.m_nMotorChannel,
                                                           _config.m_ChassisPiston_Configuration.positions,
                                                           _config.m_ChassisPiston_Configuration.m_strControllerName);
            _devicePCRCycler   = new devicePCRCycler(_tecBoard, _config.m_TEC_Channel_Configurations["TEC_5"]);
            _devicePump        = new devicePump(_motorBoard, _config.m_Pump_Configuration.channel);
            _deviceOpticsMotor = new deviceOpticsMotor(motorControllers[_config.m_OpticsMotor_Configuration.m_strControllerName],
                                                       _config.m_OpticsMotor_Configuration.m_nMotorChannel,
                                                       _config.m_OpticsMotor_Configuration.positions,
                                                       _config.m_OpticsMotor_Configuration.m_strControllerName);

            _deviceHeater = new deviceHeater(_tecBoard, _config.m_Heater_Configuration.channel);

            // Intialize the devices.
            // N.B. Home R2 before R1
            //_deviceOpticsMotor.initialize();

            /*
             * SendProgress(0.55f, "Initializing: Positioning Slider.");
             * _deviceSlider.initialize();
             * SendProgress(0.65f, "Initializing: Positioning R1 Piston.");
             * _deviceR2Piston.initialize();
             * _deviceR1Piston.initialize();
             * SendProgress(0.6f, "Initializing: Positioning R2 Piston.");
             * _deviceR1Piston.initialize1();
             * _deviceR2Piston.initialize1();
             *
             * SendProgress(0.7f, "Initializing: Positioning Heater Piston.");
             * _deviceHeaterPiston.initialize();
             * SendProgress(0.75f, "Initializing: Preparing PCR Cycler.");
             * _devicePCRCycler.initialize();
             * //SendProgress(0.8f, "Initializing: Positioning Pump.");
             * //_devicePump.initialize();
             * SendProgress(0.85f, "Initializing: Positioning Optics.");
             * //_deviceOpticsMotor.initialize();
             * SendProgress(0.9f, "Initializing: Preparing Heater.");
             * _deviceHeater.initialize();
             * SendProgress(0.95f, "Initializing: Positioning Chassis Piston.");*/
            _deviceChassisPiston.initialize();

            SendProgress(1.0f, "Initialization completed.", true);


            // Start the temperature monitor process.

            StartTempMonitor();

            return(bInitialized);
        }