private void GetMsgCountInfo() { //_syncContext.Post(AppendTextToLogMsg, "GetMsgCountInfo开启了线程:" + Thread.CurrentThread.ManagedThreadId.ToString() + "\n"); string msg = string.Empty; while (!isStopGetMsgThread) { msg = MessageQueueTool.MESSAGE_TOTAL_COUNT + "@" + MessageQueueTool.Count() + "@" + MessageQueueTool.MESSAGE_SEND_TIME; _syncContext.Post(SetMsgCountInfo, msg); Thread.Sleep(1000); } //_syncContext.Post(AppendTextToLogMsg, "GetMsgCountInfo退出了线程:" + Thread.CurrentThread.ManagedThreadId.ToString() + "\n"); }
/// 发送消息 private void SendMessage() { //_syncContext.Post(SetLabelText, "SendMessage开启了线程:" + Thread.CurrentThread.ManagedThreadId.ToString() + "\n"); AEInfos tempInfo = null; string jsonMsg = string.Empty; while (!isStopSendThread) { if (userList.Count <= 0 || MessageQueueTool.Count() <= 0) { Thread.Sleep(3000); continue; } tempInfo = MessageQueueTool.DeMessage(); jsonMsg = JsonConvert.SerializeObject(tempInfo, Formatting.Indented) + "\n"; for (int i = 0; i < userList.Count; i++) { try { userList[i].Send(Encoding.UTF8.GetBytes(jsonMsg)); } catch (Exception ex) { try { _syncContext.Post(SetLabelText, "客户端:" + userList[i].RemoteEndPoint.ToString() + "出现异常:" + ex.Message + "\n");//子线程中通过UI线程上下文更新UI userList[i].Close(); } catch { } userList.RemoveAt(i); i--; } } } //_syncContext.Post(SetLabelText, "SendMessage关闭了线程:" + Thread.CurrentThread.ManagedThreadId.ToString() + "\n"); }