我有一個C#控制檯應用程序,我有一個線程池。在線程池中將會有一個類執行一個連續的方法(直到它運行了一段時間或知道何時停止)。該方法連接到一個HttpWebResponse流,並繼續保留它。C#ThreadPool一個線程阻塞其他?
問題是,在很短的時間內,所有的線程只是做他們的事情,但只有一個線程繼續顯示它的輸出,其餘的正在等待該線程。
這是爲每個線程執行的方法。
function void ReadStream()
byte[] buf = new byte[8192];
String domain = streamingUrl
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(domain);
HttpWebResponse response = (HttpWebResponse)
request.GetResponse();
Stream resStream = response.GetResponseStream();
string tempString = null;
int count;
do
{
// fill the buffer with data
count = resStream.Read(buf, 0, buf.Length);
if (count != 0)
{
tempString = Encoding.ASCII.GetString(buf, 0, count);
try
{
mySqlManager.SaveResult(tempString);
Console.WriteLine("Been here");
Thread.Sleep(1000);
}
catch (Exception e)
{
}
}
}
while (count > 0);
}
當應用程序處於其他線程在一個線程中等待的狀態時,您可以在調試器中擊中全部。這可能會向您顯示這些線程正在等待的內容。同時檢查空的catch塊中的任何異常,這可能會隱藏一些有趣的內容。 – 2011-06-10 17:03:56
不應該使用new關鍵字創建streamReader的新實例嗎? – 2011-06-10 17:04:27