using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using GcDevicePc.Common;

namespace GcDevicePc.ProThread
{
    class DisplayDataTh
    {
        /// <summary>
        /// 单实例对象
        /// </summary>
        private static DisplayDataTh instance = null;  //静态私有成员变量
        /// <summary>
        /// 单实例对象锁
        /// </summary>
        private static object _lock = new object();

        /// <summary>
        /// 数据锁
        /// </summary>
        private static object _datalock = new object();

        #region 单实例实现
        public static DisplayDataTh GetInstance()
        {
            if (instance == null)
            {
                lock (_lock)
                {
                    if (instance == null)
                    {
                        instance = new DisplayDataTh();
                    }
                }
            }
            return instance;
        }
        #endregion

        #region 分析仪数据读取线程
        private Thread _tGetDisplayData;     //分析仪读取线程
        private ManualResetEvent displaydata_mre = new ManualResetEvent(false);  //信号控制量
        #endregion

        public void DisplayDataThStart()
        {
            if (displaydata_mre != null)
            {
                displaydata_mre.Reset();
            }

            if (_tGetDisplayData != null)
            {
                _tGetDisplayData.Abort();
            }

            _tGetDisplayData = new Thread(DisplayDataThProc);
            _tGetDisplayData.Priority = ThreadPriority.Highest;
            //  _tGetAnalyzerData.IsBackground = true;
            _tGetDisplayData.Start();
        }

        /// <summary>
        /// 判断线程工作状态
        /// </summary>
        /// <returns></returns>
        public bool ThreadWorking()
        {
            if (_tGetDisplayData != null)
            {
                if (_tGetDisplayData.ThreadState == ThreadState.Running || (_tGetDisplayData.ThreadState & ThreadState.WaitSleepJoin) != 0)
                    return true;
                else
                    return false;
            }
            return false;
        }

        /// <summary>
        /// 线程停止
        /// </summary>
        public void DisplayDataThStop()
        {
            displaydata_mre.Set();
        }

        /// <summary>
        /// 线程工作函数
        /// </summary>
        private void DisplayDataThProc()
        {
            while (!displaydata_mre.WaitOne(1000))
            {
                ReadDisplayData();
            }
        }


        private void ReadDisplayData()
        {
            MoudbusOperation.GetInstance().GetGCTemp();
            MoudbusOperation.GetInstance().GetValveState();
            MoudbusOperation.GetInstance().GetALLSWState();
            MoudbusOperation.GetInstance().GetFireState();
            MoudbusOperation.GetInstance().GetErrorState();
        }


    }
}
