Example #1
0
        private bool DoWork()
        {
            while (true)
            {
                if (!IsRun)
                {
                    if (_Chan != null)
                    {
                        _Chan.Close();
                    }
                    return(false);
                }



                if (_Chan.GetState() == ChannelState.Closed)
                {
                    return(true);
                }

                if (AddTaskQueue.Count > 0)//添加队列优先执行
                {
                    AddOrUpdateTask(AddTaskQueue.Dequeue());
                    continue;
                }
                if (RemoveTaskQueue.Count > 0)//删除队列优先执行
                {
                    RemoveTask(RemoveTaskQueue.Dequeue());
                    continue;
                }



                _Chan.ClearRecBuffer();
                _Chan.ClearSendBuffer();
                if (FirstTaskQueue.Count > 0)//高优先级指令优先执行
                {
                    temp           = FirstTaskQueue.Dequeue();
                    temp.Priority  = TaskPriority.Normal;
                    temp.IsSuccess = false;
                    sendAction(temp, _Chan);
                    byte[] a = receiveAction(temp, _Chan);
                    if (a.Length > 0)
                    {
                        temp.IsSuccess = true;
                    }
                    temp.SetRX(a);
                    AddOrUpdateTask(temp);
                    //temp.SetRX(receiveAction(temp,_Chan));
                    //temp.IsSuccess = true;
                    //AddOrUpdateTask(temp);
                }
                else if (GetNextTask(ref temp))//普通指令执行
                {
                    if (temp.ExecuteOnce && temp.IsSuccess)
                    {
                        continue;
                    }
                    temp.IsSuccess = false;
                    sendAction(temp, _Chan);
                    byte[] a = receiveAction(temp, _Chan);
                    if (a.Length > 0)
                    {
                        temp.IsSuccess = true;
                    }
                    temp.SetRX(a);
                    AddOrUpdateTask(temp);
                    //temp.SetRX(receiveAction(temp, _Chan));
                    //temp.IsSuccess = true;
                    //AddOrUpdateTask(temp);
                }
                else//没有通讯 时
                {
                    Thread.Sleep(20);
                }



                ErrCount = 0;
            }
        }