So i start a thread timer and it works but the method in the thread timer reads XML from a yahoo API, sometimes the response to the server is bad, so an XMLexception is thrown. But it seems this stops the thread or something because the thread starts good but when as fast as an exception is thrown all the threads stop. So what´s wrong , could i just start a thread every time another one is stopped? Because all the threads stops and then no threads is started agiend.
try
{
System.Threading.Timer timer;
timer = new System.Threading.Timer(new TimerCallback(TimerHelper), null, 0, 3000);
}
catch (Exception)
{
}
The TimerHelper(object obj) has try catch for exceptions.
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)
{
}
}
Here is a sample output the values are traced in 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).
Why does it exit?