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 HmiStatueTh
    {

        /// <summary>
        /// 单实例对象
        /// </summary>
        private static HmiStatueTh instance = null;  //静态私有成员变量
        /// <summary>
        /// 单实例对象锁
        /// </summary>
        private static object _lock = new object();

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

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

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

        public void HmiStatueThStart()
        {
            if (hmistatus_mre != null)
            {
                hmistatus_mre.Reset();
            }

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

            _tGetHmiStatue = new Thread(HmiStatueThProc);
            _tGetHmiStatue.Priority = ThreadPriority.Highest;
            //  _tGetAnalyzerData.IsBackground = true;
            _tGetHmiStatue.Start();

        }

        


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

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

        /// <summary>
        /// 线程工作函数
        /// </summary>
        private void HmiStatueThProc()
        {
            while (!hmistatus_mre.WaitOne(1000))
            {
                ReadHmiStatue();
            }
        }

        private void ReadHmiStatue()
        {
            MoudbusOperation.GetInstance().GetHMIStatusData();
        }

    }
}
