Esempio n. 1
0
        /// <summary>
        /// 更新UI界面显示
        /// strMsg      - 界面显示的提示字符
        /// secondDelay - 总显示时间,单位秒,若为正则显示剩余时间,若为负则循环显示滚动字符
        /// </summary>
        /// <param name="strMsg"></param>
        /// <param name="secondDelay"></param>
        /// <returns></returns>
        private CancellationTokenSource UpdateUITask(string strMsg, int secondDelay)
        {
            CancellationTokenSource tokenSource;

            if (secondDelay > 0)
            {
                tokenSource = new CancellationTokenSource(secondDelay * 1000);
            }
            else
            {
                tokenSource = new CancellationTokenSource();
            }
            CancellationToken token = tokenSource.Token;

            Task.Factory.StartNew(() => {
                int total = secondDelay;
                int count = 0;
                while (!token.IsCancellationRequested)
                {
                    try {
                        if (secondDelay > 0)
                        {
                            this.Invoke((EventHandler) delegate {
                                this.lblInfo.ForeColor = Color.Black;
                                if (count == 0)
                                {
                                    this.lblInfo.Text = strMsg + "...";
                                    m_strInfo         = this.lblInfo.Text;
                                }
                                else
                                {
                                    this.lblInfo.Text = strMsg + ",剩余" + (total - count).ToString() + "秒";
                                    m_strInfo         = this.lblInfo.Text;
                                }
                            });
                        }
                        else
                        {
                            string progress = "";
                            switch (count % 4)
                            {
                            case 0:
                                progress = "-";
                                break;

                            case 1:
                                progress = "\\";
                                break;

                            case 2:
                                progress = "|";
                                break;

                            case 3:
                                progress = "/";
                                break;
                            }
                            this.Invoke(new Action(() => {
                                this.lblInfo.Text = strMsg + "..." + progress;
                                m_strInfo         = this.lblInfo.Text;
                            }));
                        }
                    } catch (ObjectDisposedException ex) {
                        m_log.TraceWarning(ex.Message);
                    }
                    if (secondDelay > 0)
                    {
                        Thread.Sleep(1000);
                        if (total > count)
                        {
                            ++count;
                        }
                    }
                    else
                    {
                        ++count;
                        Thread.Sleep(500);
                    }
                }
            }, token);
            return(tokenSource);
        }
Esempio n. 2
0
        /// <summary>
        /// 获取I-7033的温度值,同步函数,会阻塞线程
        /// 以string[]格式返回,[通道0, 通道1]
        /// </summary>
        public string[] GetTemper()
        {
            // 获取当前连接状态
            bool bConnected = TestConnect();

            // 若断线的话重连
            if (!bConnected)
            {
                SafeClose();
                bConnected = TCPClientInit();
                if (!bConnected)
                {
                    return(SplitTemper(""));
                }
            }

            DateTime before = DateTime.Now;
            TimeSpan interval;

            // 发送读取温度命令
            string strMsg = "#01\r";

            byte[] sendMsg = Encoding.ASCII.GetBytes(strMsg);
            try {
                m_clientStream.Write(sendMsg, 0, sendMsg.Length);
                m_log.TraceInfo("TcpClient sent: " + strMsg.Replace("\r", "\\r"));
            } catch (Exception ex) {
                m_log.TraceError("TcpClient sending error: " + ex.Message);
                m_client.Close();
                bConnected = TCPClientInit();
            }

            // 接收温度值
            int    bytesRead;
            string strRecv = "";

            if (bConnected)
            {
                try {
                    while (!strRecv.Contains("\r"))
                    {
                        interval = DateTime.Now - before;
                        if (interval.TotalMilliseconds > m_cfg.Setting.Data.Interval * TimeoutScale)
                        {
                            m_log.TraceWarning("TcpClient timeout receieved: " + strRecv.Replace("\r", "\\r"));
                            throw new ApplicationException("TcpClient receieving timeout");
                        }
                        while (m_clientStream.DataAvailable)
                        {
                            bytesRead = m_clientStream.Read(m_recvBuf, 0, m_bufSize);
                            strRecv  += Encoding.ASCII.GetString(m_recvBuf, 0, bytesRead);
                        }
                    }
                    m_log.TraceInfo("TcpClient receieved: " + strRecv.Replace("\r", "\\r"));
                } catch (Exception ex) {
                    m_log.TraceError("TcpClient receieving ERROR: " + ex.Message);
                }
            }
            else
            {
                m_log.TraceError("TcpClient can't connect server");
            }
            return(SplitTemper(strRecv));
        }