2014-01-17 24 views
0
 Try 
      Do 
       read = stream.Read(datain, 0, datain.Length) 
       responseData = System.Text.Encoding.ASCII.GetString(datain, 0, read) 
       finalResponse = finalResponse + responseData 
      Loop While read > 0 
     Catch ex As Exception 

有時程序停留在該stream.read如果失敗,我想moveon。如何指定stream.read的超時函數?

如何解決這個問題?

+0

什麼類型是'stream'? –

回答

0

您可以設置流的readtimeout值。

stream.BaseStream.ReadTimeout = 1000 

您可以在MSDN

對房地產 here電文讀出
0

你必須使用stream.ReadTimeout(以毫秒爲單位的時間)

而且你必須使用一個嘗試捕捉agains TimeoutException異常

Try 
      stream.ReadTimeout(5000); 
      Do 
    try 
    { 
       read = stream.Read(datain, 0, datain.Length) 
       responseData = System.Text.Encoding.ASCII.GetString(datain, 0, read) 
       finalResponse = finalResponse + responseData 
    } 
    catch err as TimeoutException 
    { 
      read = 1; //To avoid quiting the loop 
    } 
      Loop While read > 0 
     Catch ex As Exception 

也許我的語法並不完美,因爲我在C#中編碼,但兩種語言的溶劑仍然相同。