private void RecieveMethod(ThreadExPara para) { byte[] buffer = new byte[4096]; int recCount; this.SendWelcom(); while (!para.Token.IsCancellationRequested) { try { recCount = this._client.Receive(buffer, buffer.Length, SocketFlags.None); if (recCount == 0) { continue; } if (recCount < buffer.Length) { buffer = buffer.Take(recCount).ToArray(); } this._receiveQueue.Enqueue(buffer); } catch (SocketException) { this.ClientClose(); break; } catch (Exception ex) { Loger.Error(ex); } } }
private void StartListen(ThreadExPara para) { Socket client; while (!para.Token.IsCancellationRequested) { //client = this._listenerSocket.Accept(); client = this._tcpListener.AcceptSocket(); lock (this._clientsLock) { this._clients.Add(new TelnetClient(this._authInfo, this._serviceName, client, this._proCallback, this.ClientClose)); } } }
/// <summary> /// 定时操作 /// </summary> /// <param name="para">线程参数</param> private void TimingThreadMethod(ThreadExPara para) { LoopLinked <AlarmTime> alarmLoopLinked = this.CreateAlarmLoopLinked(); if (alarmLoopLinked.Count == 0) { throw new Exception("没有添加时间点"); } // 当前执行次数 int excuteCount = 0; LoopLinkedNode <AlarmTime> currentNode = alarmLoopLinked.FirstNode; TimeSpan tsWait; while (true) { tsWait = this.CaculateWaitTime(currentNode.Value.Time); //如果停止门铃执行 if (para.Token.IsCancellationRequested) { break; } Thread.Sleep(tsWait); //如果停止门铃执行 if (para.Token.IsCancellationRequested) { break; } //响铃 this.OnRing(currentNode.Value.Time); //执行次数验证,如果不为无限次,那么当执行的次数超过要执行的总次数时,就停止 if (this._count != -1) { excuteCount++; if (excuteCount >= this._count) { break; } } currentNode = currentNode.Next; } }
/// <summary> /// 重写执行线程方法 /// </summary> /// <param name="para">线程参数</param> private void ExcuteThreadMethod(ThreadExPara para) { //执行数据处理方法 long count = 0; while (true) { try { //获取等待处理的数据项数 count = this._priorityQueue.Count; //如果线程取消 if (para.Token.IsCancellationRequested) { break; } //如果等待处理的数据项数数为0.则等待信号的输入 if (count == 0) { this._autoResetEvent.WaitOne(); continue; } //数据处理 this.OnRaiseDataProcess(this._priorityQueue.Dequeue()); //如果线程取消 if (para.Token.IsCancellationRequested) { break; } } catch (ThreadAbortException taex) { Loger.Info(taex.Message); } catch (Exception ex) { Loger.Error(ex); } } }
private void ProcessThreadMethod(ThreadExPara para) { try { const int millisecondsTimeout = 1000; T item; while (!para.Token.IsCancellationRequested) { try { try { if (!this._blockingCollection.TryTake(out item, millisecondsTimeout, para.Token)) { continue; } } catch (OperationCanceledException) { break; } catch (ArgumentNullException) { continue; } catch (ObjectDisposedException) { break; } this.AddNewProcessThread(); this._process(item); } catch (Exception exi) { Loger.Error(exi); } } } catch (Exception ex) { Loger.Error(ex); } }
private void ListenThreadMethod(ThreadExPara para) { try { while (!para.Token.IsCancellationRequested) { try { using (NamedPipeServerStream pipeServerStream = new NamedPipeServerStream(this._pipeName)) { try { pipeServerStream.WaitForConnection(); if (para.Token.IsCancellationRequested) { pipeServerStream.Close(); break; } byte[] data = ReadData(this._pipeName, pipeServerStream, this._readDataMillisecondsTimeout, para.Token); if (para.Token.IsCancellationRequested) { break; } //回调操作-各种处理 byte[] result = this._proFunc?.Invoke(data); if (para.Token.IsCancellationRequested) { break; } WriteData(pipeServerStream, result); } catch (TimeoutException) { //请求方未知原因未即时发送数据导致读取数据超时,多半是请求方关闭 Loger.Debug("TimeoutException"); } catch (IOException) { //请求方已关闭 Loger.Debug("IOException"); } catch (Exception exi) { Loger.Warn(exi); } } } #if NET4_0 catch (IOException ioex) when(ioex.Message.Contains("-2147024665")) #else catch (IOException ioex) when(ioex.HResult == -2147024665) #endif { Loger.Debug(ioex); //所有的管道范例都在使用中,等待一段时间重试 Thread.Sleep(100); } } } catch (Exception ex) { Loger.Error(ex); } }