コード例 #1
0
        /// <summary>
        /// 线程池验证超时的回调函数
        /// </summary>
        /// <param name="ir">异步操作的对象</param>
        private void VaildateTimeOut(object obj)
        {
            OperTimeOut tempObj = obj as OperTimeOut;

            if (tempObj != null)
            {
                while (!tempObj.IsSuccessful)
                {
                    Thread.Sleep(100);
                    if ((DateTime.Now - tempObj.StartTime).TotalMilliseconds > tempObj.DelayTime)
                    {
                        if (!tempObj.IsSuccessful)
                        {
                            //如果检测超时直接关闭释放
                            tempObj.WorkOPc.Disconnect();
                        }
                        break;
                    }
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// 创建连接Opc
        /// </summary>
        /// <param name="OpcHostName">服务器的主机名</param>
        /// <param name="OpcHostIP">服务器的ip</param>
        /// <param name="timeOut">超时时间</param>
        /// <returns>自定义返回集合</returns>
        public Result <OPCServer> CreateConnection(string OpcHostName, string OpcHostIP, int timeOut)
        {
            OPCServer          opc         = null;
            Result <OPCServer> result      = new Result <OPCServer>();
            ManualResetEvent   connectDone = new ManualResetEvent(false);
            StateObject        state       = new StateObject();

            try
            {
                opc = new OPCServer();
            }
            catch (Exception ex)
            {
                result.IsSuccess = false;
                result.Message   = ex.Message + "初始化Opc服务器异常!";
                return(result);
            }
            OperTimeOut tempTimeOut = new OperTimeOut()
            {
                WorkOPc   = opc,
                DelayTime = timeOut,
                StartTime = DateTime.Now,
            };

            //开启线程验证连接是否超时
            ThreadPool.QueueUserWorkItem(new WaitCallback(VaildateTimeOut), tempTimeOut);

            try
            {
                state.WorkOpc  = opc;
                state.WaitDone = connectDone;
                state.Ip       = OpcHostIP;
                state.Name     = OpcHostName;
                ThreadPool.QueueUserWorkItem(new WaitCallback(ThradConnecedOpc), state);
            }
            catch (Exception ex)
            {
                tempTimeOut.IsSuccessful = true;
                opc.Disconnect();
                connectDone.Close();
                result.Message = ex.Message;
                return(result);
            }
            connectDone.WaitOne();
            connectDone.Close();
            tempTimeOut.IsSuccessful = true;
            //如果开启线程发生了异常
            if (state.IsError)
            {
                result.Message   = state.ErrerMsg;
                result.IsSuccess = false;
                if (opc != null)
                {
                    opc.Disconnect();
                }
            }
            if (state.OpcStatus)
            {
                result.Content   = opc;
                result.IsSuccess = true;
                state.Clear();
                state = null;
            }
            return(result);
        }