我猜这并不因为StreamReader的是非线程安全的工作,(不知道HOWTO解决这个问题,谷歌是没有帮助)StreamReader线程安全问题?可能?
反正我一直在试图弄清楚究竟什么错这个代码在80%的时间内工作,其他时候它无法解析传入的数据包,并且会丢弃它们。
这是一个类似于http的tcp服务器的虚拟写入。它的工作方式与http数据包完全相同,但“CONTENT-LENGTH”标题告诉它数据包数据(有效负载)的长度。这是问题发生的地方。任何人都可以向我建议如何改善这一点并解决这个问题?因为我完全失去了。
void InternalStart()
{
bool continueWhile = true;
while (continueWhile)
{
if (SR.EndOfStream)
{
continueWhile = false;
break;
}
if (par_ReadStatus != ReadStatusEnum.WaitingForPayload)
{
int charCode = SR.Peek();
if (charCode == -1)
{
continueWhile = false;
break;
}
string outputLine = "";
outputLine = SR.ReadLine();
ReadLine(outputLine);
}
else if (par_ReadStatus == ReadStatusEnum.WaitingForPayload)
{
int length = int.Parse(par_ParsingPacket.Attributes["CONTENT-LENGTH"]);
char[] array = new char[length];
for (int i = 0; i < length; i++)
{
array.SetValue(Convert.ToChar(SR.Read()), i);
}
string payload = new string(array);
ReadLine(payload);
}
}
if (ReadEnd != null)
{
ReadEnd();
}
}
您能否提供有关您遇到的错误的更多详细信息? – 2012-02-05 07:29:01
[StreamWriter多线程C#]的可能重复(http://stackoverflow.com/questions/19304209/streamwriter-multi-threading-c-sharp) – Liam 2015-04-28 14:13:34