FrmPvsServer.cs 8.29 KB
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace SunVoteARSPPT
{
    public delegate void OnPvsBaseConnectEvent(int ID, int iStatus);
    public delegate void OnPvsBaseEvent(int ID, int iMode, string sInfo);
    public delegate void OnPvsKeyEvent(int ID, int iMode, string sInfo);
    public delegate void OnPvsKeyVote(int ID, int iMode, string sInfo);
    public delegate void OnPvsCommErrSys(string ID, int ErrNo, string ErrInfo);
    public delegate void OnPvsCommErrEx(string ID, int ErrNo, string ErrInfo);

    public partial class FrmPvsServer : Form
    {
        public FrmPvsServer()
        {
            InitializeComponent();
        }

        private static FrmPvsServer frmPvs = null;

        public static PVSServer.MyPVS GetMyPvs()
        {
            return GetFrmPvs().InitPvs();
        }

        public static FrmPvsServer GetFrmPvs()
        {
            if (frmPvs == null)
            {
                frmPvs = new FrmPvsServer();
                frmPvs.Show();
            }
            return frmPvs;
        }

        /// <summary>
        /// 释放PVS,杨斌 2015-04-14
        /// </summary>
        public static void FreeMyPvs()
        {
            if (frmPvs != null)
            {
                frmPvs.PvsStop();
                frmPvs.PVS.BaseConnectClose();
                frmPvs.PVS = null;
                frmPvs.Close();
                frmPvs = null;
                KillProcess("PVSServer");
                GC.Collect();
            }
        }

        public static void KillProcess(string processName)
        {
            try
            {                
                foreach (Process thisproc in Process.GetProcessesByName(processName))//循环查找    
                {
                    //if (!thisproc.CloseMainWindow())
                    {
                        thisproc.Kill();
                    }
                }
            }
            catch { }
        }

        private PVSServer.MyPVS PVS = null;

        public OnPvsBaseConnectEvent OnBaseConnectEvent = null;
        public OnPvsBaseEvent OnBaseEvent = null;
        public OnPvsKeyEvent OnKeyEvent = null;
        public OnPvsKeyVote OnKeyVote = null;
        public OnPvsCommErrSys OnCommErrSys = null;
        public OnPvsCommErrEx OnCommErrEx = null;

        private void FrmPvsServer_Load(object sender, EventArgs e)
        {
            this.Hide();
            this.ShowInTaskbar = false;
        }

        private PVSServer.MyPVS InitPvs()
        {
            if ((PVS == null) && (IntPtr.Size == 4))
            {
                PVS = new PVSServer.MyPVS();

                PVS.BaseEvent += PVS_BaseEvent;
                PVS.BaseConnectEvent += PVS_BaseConnectEvent;
                PVS.CommErrEx += PVS_CommErrEx;
                PVS.CommErrSys += PVS_CommErrSys;
                PVS.KeyEvent += PVS_KeyEvent;
                PVS.KeyVote += PVS_KeyVote;

                tmrGet.Elapsed += tmrGet_Elapsed;
                //tmrGet.Tick += tmrGet_Tick;
            }

            return PVS;
        }

        private void FrmPvsServer_FormClosed(object sender, FormClosedEventArgs e)
        {
            try
            {
                PvsStop();//杨斌 2015-04-14
                PVS = null;
            }
            catch { }
        }

        void PVS_BaseConnectEvent(int ID, int iStatus)
        {
            if (OnBaseConnectEvent != null)
                this.BeginInvoke(OnBaseConnectEvent, ID, iStatus);
            //this.BeginInvoke(new OnPvsBaseConnectEvent(PVS_PvsOnBaseConnectEvent), ID, iStatus);
        }

        void PVS_BaseEvent(int ID, int iMode, string sInfo)
        {
            if (OnBaseEvent != null)
                this.BeginInvoke(OnBaseEvent, ID, iMode, sInfo);
        }

        void PVS_KeyEvent(int ID, int iMode, string sInfo)
        {
            if (OnKeyEvent != null)
                this.BeginInvoke(OnKeyEvent, ID, iMode, sInfo);
        }

        /// <summary>
        /// 收到按键值缓冲。避免卡死。杨斌 2015-03-27
        /// </summary>
        private List<KeyVoteRes> LstGetKeyVote = new List<KeyVoteRes>();

        void PVS_KeyVote(int ID, int iMode, string sInfo)
        {
            //杨斌 2015-03-27
            //if (OnKeyVote != null)
            //    this.BeginInvoke(OnKeyVote, ID, iMode, sInfo);

            if (VoteModifyMode == 0)//不允许修改,PVS键盘不支持,软件屏蔽。杨斌 2015-11-10
            {
                string sID = ID.ToString();
                if (GlobalInfo.response.ResponseDataList.Contains(sID))
                {
                    if (!string.IsNullOrEmpty(GlobalInfo.response.ResponseDataList[sID].KeyValue))
                        return;
                }
                if (LstGetKeyVote.Exists(o => o.ID == ID))
                {
                    return;
                }
            }

            LstGetKeyVote.Add(new KeyVoteRes() { ID = ID, iMode = iMode, sInfo = sInfo });
            if (!tmrGet.Enabled)
            {
                tmrGet.Interval = 10;
                tmrGet.Enabled = true;
            }
        }

        void PVS_CommErrSys(string ID, int ErrNo, string ErrInfo)
        {
            if (OnCommErrSys != null)
                this.BeginInvoke(OnCommErrSys, ID, ErrNo, ErrInfo);
        }

        void PVS_CommErrEx(string ID, int ErrNo, string ErrInfo)
        {
            if (OnCommErrEx != null)
                this.BeginInvoke(OnCommErrEx, ID, ErrNo, ErrInfo);
        }

        private int VoteModifyMode = 1;
        public void VoteStart(int voteMode, string voteSetting, int iModifyMode = 1)
        {
            VoteModifyMode = iModifyMode;
            Action<object> action = (obj) => PvsStart(voteMode, voteSetting);
            action.BeginInvoke(this, ar => action.EndInvoke(ar), null);
        }

        public void VoteStop()
        {
            Action<object> action = (obj) => PvsStop();
            action.BeginInvoke(this, ar => action.EndInvoke(ar), null);
        }

        private void PvsStart(int voteMode, string voteSetting)
        {
            if (PVS != null)
            {
                PVS.VoteMode(voteMode, voteSetting);
                PVS.VoteNew();
                PVS.VoteStart();
            }
        }

        private void PvsStop()
        {
            if (PVS != null)
            {
                PVS.VoteStop();
                tmrGet.Stop();
                //IsRunGet = false;
                //GetData(9999);
            }
        }

        System.Timers.Timer tmrGet = new System.Timers.Timer();
        //System.Windows.Forms.Timer tmrGet = new System.Windows.Forms.Timer();

        private bool IsRunGet = false;

        //private void tmrGet_Tick(object sender, EventArgs e)
        void tmrGet_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (IsRunGet)
                return;

            GetData();

            //Control.CheckForIllegalCrossThreadCalls = false;
            //Thread th = new Thread(GetData);
            //th.IsBackground = true;
            //th.Start();       
        }

        private void GetData(int count = 10)
        {
            IsRunGet = true;

            while ((LstGetKeyVote.Count > 0) && (count > 0))
            {
                count--;

                int ID = LstGetKeyVote[0].ID;
                int iMode = LstGetKeyVote[0].iMode;
                string sInfo = LstGetKeyVote[0].sInfo;

                LstGetKeyVote.RemoveAt(0);

                //异步调用,卡UI
                //if (OnKeyVote != null)
                //    this.BeginInvoke(OnKeyVote, ID, iMode, sInfo);

                //直接调用,但是线程访问问题,卡UI。
                //if (OnKeyVote != null)
                //    OnKeyVote(ID, iMode, sInfo);

                //方法A:同步调用,但是卡UI
                if (OnKeyVote != null)
                    this.Invoke(OnKeyVote, ID, iMode, sInfo);

            }

            if (LstGetKeyVote.Count < 1)
                tmrGet.Enabled = false;

            IsRunGet = false;
        }

        private class KeyVoteRes
        {
            public int ID = 0;
            public int iMode = 0;
            public string sInfo = "";
        }
    }
}