首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

多个客户端 不定时向服务器端发送数据 服务器端美的处理方式

2012-09-10 
多个客户端 不定时向服务器端发送数据 服务器端好的处理方式自己写的多线程处理 每次cpu占用太高private v

多个客户端 不定时向服务器端发送数据 服务器端好的处理方式
自己写的多线程处理 每次cpu占用太高
private void ClientConn()
  {
  int threadNum = 0;
  while (true)
  {
  if (threadList.Count() != 0)
  {
  Client client = threadList[threadNum];
  client.islive = true;
  string[] str;
  client.RecMsgFormClient();
  str = client.GetMessage().Split(new char[]{'|'});
  switch (str[0])
  {
  case "Login":
  while (true)
  {
  if (!connIsLock)
  {
  connIsLock = true;
  client.InfoLogSuc(client.GetMessage(), mscn);
  client.UpdateClientDetailInfo(mscn);
  break;
  }
  }
  connIsLock = false;
  SyncRoomsStatus();
  continue;
  case "RoomNum":
  RegisterRoomNum(client,str[1]);
  continue;
  case "RoomStatus":
  while(true)
  {
  if(!roomUserNumIsLock)
  {
  roomUserNumIsLock = true;
  int i = int.Parse(str[1]) - 1;
  roomUserNum[i] += int.Parse(str[2]);
  break;
  }
  }
  roomUserNumIsLock = false;
  SyncRoomsStatus();
  continue;
  case "LoginOut":
  continue;
  case "Pre":
  client.bIsPre = bool.Parse(str[1]);
  CheckSameRoomIsPre(client);
  continue;
  case "CancelPre":
  client.bIsPre = bool.Parse(str[1]);
  CheckRoomBothPre = true;
  continue;
  case "DiceTally":
  SavePlayerDiceTally(client,client.GetMessage());
  continue;
  case "Exit":
  threadList.Remove(client);
  client.islive = false;
  client.thClient.Abort();
  onLineNum--;
  continue;
  default :
  continue;
  }
  }
  threadNum++;
  if (threadNum >= threadList.Count())
  {
  threadNum = 0;
  }
  Thread.Sleep(100);
  }  


  }

[解决办法]
每一个While(true)中适当的放入休眠,试试看

热点排行