Search code examples
c#multithreadingstack-overflow

Can't see the infinite loop


So I'm working on a autoupdating server list that loads all server ip:port combos and displays them into a listview. I am doing all this within a thread so I need to invoke my controls to do things to them. Here is my invoke method..

    public void invoke(MethodInvoker m) {
        try { invoke(m); } catch {} 
    }

This is my updating thread method:

    public void updater()
    {
        while (autoUpdate)
        {
            reader = new StreamReader("servers.list");
            servers = reader.ReadToEnd().Split('\n');
            reader.Close();

            invoke(new MethodInvoker(delegate { list.Clear(); }));

            foreach (String s in servers)
            {
                String[] part = new String[] { s };
                invoke(new MethodInvoker(delegate { list.Items.Add("Server Name").SubItems.AddRange(part); }));
            }

            Thread.Sleep(5000);
        }
    }

Does anyone have any idea why it is doing this? I have made applications like this before and haven't come across this problem. Maybe I am missing something but any help will be appreciated.

Thanks.


Solution

  • You need to change it to:

    public void invoke(MethodInvoker m) 
    {
       try { someControl.Invoke(m); } catch {} 
    }