0

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?

2 Answers2

0

Do not catch generic Exception because using (code from you post)

catch (Exception) { }

can lead to situation where every possible Exception slips thru your fingers.

Try to figure out where System.Net.WebException might occur and name your threads so you have more insight in what's going on, e.g.:

(new Thread(() => {}) { Name = "Some thread" }).Start();

outputs

The thread 'Some thread' (0x260c) has exited with code 0 (0x0).

And in case you wonder what those first chance exceptions are, peek here http://blogs.msdn.com/b/davidklinems/archive/2005/07/12/438061.aspx

As @BlackICE stated, please post the missing piece of code so your problem can be solved.

Edit:

Are you sure you are looking for right place to solve this. As stated, thread exits are not sign of problem (you do want the threads to exit when their work is done) nor first chance exceptions (exception was thrown but handled).

Could it be CGd timer issue, as in this post?

System.Threading.Timer not firing after some time

Edit:

Try something like this, m'kay? It will run forever in a non-blocking fashion. Just add some more detailed exception handling ;).

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;
        Task.Factory.StartNew(() => DoTheMagic());
    }

    // Here the magic occurs
    public async Task DoTheMagic()
    {
        while (true)
        {
            string url = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22YHOO%22)&diagnostics=true&env=http://datatables.org/alltables.env";
            string raw = await GetResponse(url);
            var parsed = await ParseXml(raw);

            // Pass results to UI
            Dispatcher.BeginInvoke(new Action(() =>
                {
                    if (parsed == null || !parsed.Any())
                        return;

                    var now = DateTime.Now;
                    foreach (var tuple in parsed)
                        textBox.Text += string.Format("{0} {1} = {2}{3}", now, tuple.Item1, tuple.Item2, Environment.NewLine);
                }));
            Thread.Sleep(3000);
        }
    }

    // Get response from service
    public async Task<string> GetResponse(string url)
    {
        string content = null;
        try
        {
            var request = WebRequest.Create(url);
            var response = await request.GetResponseAsync();
            var stream = response.GetResponseStream();

            if (stream == null)
                return null;

            using (var reader = new StreamReader(stream, Encoding.GetEncoding("utf-8")))
                content = await reader.ReadToEndAsync();
        }
        catch
        {
            System.Diagnostics.Debug.WriteLine("ZOMG!");
        }

        return content;
    }

    // Parse response with your logic
    public Task<List<Tuple<string, string>>> ParseXml(string xml)
    {
        return Task.Factory.StartNew(() =>
            {
                try
                {
                    var nodes = new string[] { "Symbol", "Ask", "Bid" };
                    var result = new List<Tuple<string, string>>();

                    using (var reader = new XmlTextReader(new StringReader(xml)))
                    {
                        reader.MoveToContent();
                        while (reader.Read())
                        {
                            if (reader.NodeType.Equals(XmlNodeType.Element))
                            {
                                if (nodes.Contains(reader.LocalName))
                                    result.Add(new Tuple<string, string>(reader.LocalName, reader.ReadElementContentAsString()));
                            }
                        }
                    }
                    return result;
                }
                catch
                {
                    System.Diagnostics.Debug.WriteLine("I haz exception!!1");
                    return null;
                }
            });
    }
}

Output

notfound

Community
  • 1
  • 1
Mikko Viitala
  • 8,344
  • 4
  • 37
  • 62
  • Yes ofcourse i wan´t them to exit but why does the thread timer stop creating threads after a while ? @mikkoviitala – MattiasLarsson Feb 24 '14 at 19:17
  • Follow the link in my post, @MattiasLarsson, follow the link :). Here's the answer from Skeet I think could help you http://stackoverflow.com/a/3635896/1061668 (maybe) – Mikko Viitala Feb 24 '14 at 19:20
  • I tried (new Thread((TimerHelper) => { }) { Name = "Time" }).Start(); didn´t work , Output: The thread 'Time' (0xf88) has exited with code 0 (0x0). Nothing more, it seems it didn´t even run TimerHelper(); – MattiasLarsson Feb 24 '14 at 21:57
  • How you came to conclusion it did not run? It just outputted it exited the thread? – Mikko Viitala Feb 24 '14 at 22:08
  • Little mistake you have there @MattiasLarsson Try `(new Thread(TimerHelper) { Name = "Time" }).Start();` instead. Also, I will edit my answer to give you a hint on how to refactor away from your current solution (if it's any good for you). – Mikko Viitala Feb 24 '14 at 22:15
  • Ohh relly thanks, that´s to nice. I Will have a look at it, i tried a background worker and i put a while loop in there so that it would run for ever work better. It works more solid but it still stops after a while, it exit´s the thread after a while. I will have a look at your code tomorow i have school tomorw have to go to bed ;) Relly THANKS agiend. – MattiasLarsson Feb 24 '14 at 22:34
  • No problem @MattiasLarsson, I added and edited the bit of code there just now. – Mikko Viitala Feb 24 '14 at 22:46
  • i can´t, i don´t have enoough rep. – MattiasLarsson Mar 18 '14 at 10:07
0

The thread exits are normal, look at the exit codes :

The thread '<No Name>' (0xe3c) has exited with code 0 (0x0).

This is going to happen every time the timer fires, and it looks like from your output that the timer is firing periodically (you're getting multiple threads exiting, apparently periodically). You might want to put something in your exception handlers instead of just eating them, then you can set a breakpoint and see why subsequent calls to the timer handler are failing to run your code.

BlackICE
  • 8,816
  • 3
  • 53
  • 91
  • I would like it to fire a new thread and when that thread has finished i want to fire a new one so that heres always one running and only one is this possible? @BlackICE – MattiasLarsson Feb 24 '14 at 19:16
  • why are you even using a timer then? Just make a loop that never terminates, and sleep for 100ms or something before beginning the loop again. Call this using a background worker thread. – BlackICE Feb 24 '14 at 20:00
  • Because i have tried that and a normal timer, the problem with those solutions is that when the method run it´s locks the gui, i have alredy asked about this at Stackoverflow ( Check my profile ) – MattiasLarsson Feb 24 '14 at 20:17
  • That's why you use a background thread, _and put the sleep in there_. Without the sleep it probably would hang the UI. – BlackICE Feb 24 '14 at 20:19