因此,我啓動線程計時器,它的工作方式,但線程計時器中的方法從yahoo API讀取XML,有時對服務器的響應不好,因此引發了XMLexception。但它似乎停止了線程或某事,因爲線程開始良好,但是當拋出異常時,所有線程都停止。那麼有什麼不對,我可以在每次停止另一個時啓動一個線程?因爲所有的線程停止,然後沒有線程開始agiend。線程停止,XMLexception錯誤
try
{
System.Threading.Timer timer;
timer = new System.Threading.Timer(new TimerCallback(TimerHelper), null, 0, 3000);
}
catch (Exception)
{
}
TimerHelper(object obj)有try catch的異常。
public void TimerHelper(object obj)
{
try
{
if (dataGridView.Rows.Count > 0 && !(dataGridView.Rows[0].Cells[0].Value == null)) // Updates the rows idividualy as long as first row is not empty
{
string[] cells;
cells = new string[dataGridView.Columns.Count * dataGridView.Rows.Count];
for (int i = 0; i < dataGridView.RowCount; i++)
{
if (!(dataGridView.Rows[i].Cells[0].Value == null || dataGridView.Rows[i].Cells[0].Value.ToString() == "-")) // Makes sure that the row to update is not an empty row
{
String symbol;
symbol = Convert.ToString(dataGridView.Rows[i].Cells[0].Value);
String URLString2 = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22" + symbol + "%22)%0A%09%09&diagnostics=true&env=http%3A%2F%2Fdatatables.org%2Falltables.env";
for (int t = 2; t < dataGridView.Columns.Count; t++)
{
dataGridView.SuspendLayout();
XmlTextReader reader2 = new XmlTextReader(URLString2); // Makes the reader read from the string abow (URL)
string NasdaqOpenTime = "09:00:00";
// if the market haven't been open for the day then theres no DaysLow value
if (dataGridView.Columns[t].HeaderText == "DaysLow" && DateTime.Now.CompareTo(DateTime.Parse(NasdaqOpenTime)) == -1)
{
cells[t - 2] = "0";
}
// if the market haven't been open for the day then theres no DaysHigh value
if (dataGridView.Columns[t].HeaderText == "DaysHigh" && DateTime.Now.CompareTo(DateTime.Parse(NasdaqOpenTime)) == -1)
{
cells[t - 2] = "0";
}
else
{
reader2.ReadToFollowing(dataGridView.Columns[t].HeaderText); // Reada until it fins the elemnt Bid , then stops on it
reader2.ReadStartElement(dataGridView.Columns[t].HeaderText); // Recognizes Bid as start element (Bid)
string temporary;
temporary = reader2.ReadString();
Trace.WriteLine(dataGridView.Columns[t].HeaderText + ": " + temporary);
cells[t - 2] = temporary; // Reads the text in between (Declared as a string) actualy the bid value
reader2.ReadEndElement(); // Checks that the current nod is an end element (/Bid) if so then continue
reader2.ResetState();
}
}
}
}
for (int h = 0; h < dataGridView.Rows.Count; h++)
{
for (int t = 2; t < dataGridView.Columns.Count; t++)
{
dataGridView.Rows[h].Cells[t].Value = cells[t - 2];
}
}
}
}
catch (XmlException)
{
}
catch (Exception)
{
}
}
下面是一個示例輸出,這些值在TimerHelper(object obj)中進行了跟蹤。
'FetchBySymbol.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Users\Mattias\documents\visual studio 2012\Projects\FetchBySymbol\FetchBySymbol\bin\Debug\FetchBySymbol.exe', Symbols loaded.
'FetchBySymbol.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll'
Ask: 37.44
Bid: 37.43
...
...
...
Ask: 37.44
Bid: 37.43
Ask: 37.42
Bid: 37.41
The thread '<No Name>' (0x104c) has exited with code 0 (0x0).
The thread '<No Name>' (0x1920) has exited with code 0 (0x0).
A first chance exception of type 'System.Net.WebException' occurred in System.dll
A first chance exception of type 'System.Net.WebException' occurred in System.Xml.dll
The thread '<No Name>' (0xe3c) has exited with code 0 (0x0).
The thread '<No Name>' (0x1af0) has exited with code 0 (0x0).
The thread '<No Name>' (0x1b18) has exited with code 0 (0x0).
The thread '<No Name>' (0xb90) has exited with code 0 (0x0).
The thread '<No Name>' (0x20) has exited with code 0 (0x0).
它爲什麼退出?
你是如何從雅虎API讀取XML的?你使用webclient或httprequest或其他東西?是否可以顯示代碼? – Steve
Httprequest [鏈接](http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22YHOO%22) %0A%09%09&diagnostics = true&env = http%3A%2F%2Fdatatables.org%2Falltables.env)@Steve – MattiasLarsson
你是否在ASP.NET中執行此操作。如果是的話,它可能會被線程池回收,因爲退出代碼都是0 – Steve