我有一个线程检查用户是否在线
var user = (User)userObj;
if (!IsUserOnline(user)) return;
if (!TryToConnect(user)) return;
var TCP = user.CmdTCP;
Stream stm = TCP.GetStream();
这每次都成功完成,但是当我
stm.Write(GetBytes("chk"), 0, 3);
进而
var bb = new byte[1];
try {
stm.Read(bb, 0, 1);
} catch (Exception e) { /*error*/ }
if (bb[0] == CHR_SUCCESS) {
//etc etc
}
在检查用户是否第二次在线时,流读取失败且没有警告。也不例外,代码不会走得更远。线程刚刚停止,程序工作。
整个线程:
var user = (User)userObj;
if (!IsUserOnline(user)) return;
if (!TryToConnect(user)) return;
var TCP = user.CmdTCP;
Stream stm = TCP.GetStream();
try {
stm.Write(GB("chk"), 0, 3);
toLog("refresh::chk sent ;)");
} catch (Exception e) {
#region unable to send packet, online
toLog("\trefresh::" + user.name + " is online (Connected, unable to send packet) " + e.Message);
setUserStatus(user, userStatus.Online);
return;
#endregion
}
var bb = new byte[1];
try {
stm.Read(bb, 0, 1); //<<<<<<<<< here thread just ends, no toLog("..");, no exception
toLog("refresh::got " + (char)bb[0]);
} catch (Exception e) {
#region unable to get packet, online
toLog("\trefresh::" + user.name + " is online (Connected, unable to get packet) " + e.Message);
setUserStatus(user, userStatus.Online);
return;
#endregion
}
if (bb[0] == CHR_SUCCESS) {
try {
stm.Write(GB(toStr(CHR_SSUCCESS)), 0, 1);
toLog("refresh::CHR_SSUCCESS sent ;)");
} catch (Exception e) {
#region unable to send packet 2, online
toLog("\trefresh::" user.name + " is online (Connected, unable to send packet #2) " + e.Message);
setUserStatus(user, userStatus.Online);
return;
#endregion
}
toLog("\trefresh::" + user.name + " is online");
setUserStatus(user, userStatus.Online);
} else {
for (int x = 0; x < 5; x++) {
toLog("\t\trefresh::got wrong answer");
System.Media.SystemSounds.Beep.Play();
Thread.Sleep(500);
}
setUserStatus(user, userStatus.Online);
}