111,094
社区成员




public string WriteAndRead(byte[] buffer, int offset, int count, string readFix, int timeout)
{
string response = string.Empty;
bool lockToken = false;
try
{
spinLock.Enter(ref lockToken);
TimeSpan startSpan = new TimeSpan(DateTime.Now.Ticks);
Write(buffer, 0, buffer.Length);
do
{
TimeSpan doSpan = new TimeSpan(DateTime.Now.Ticks);
double millis = doSpan.Subtract(startSpan).TotalMilliseconds;
response += Read();
if (millis >= timeout)
{
response = string.Empty;
break;
}
}
while (!response.EndsWith(readFix));
}
finally
{
if (lockToken)
spinLock.Exit(lockToken);
}
return response;
}
我目前是这样解决的.