1

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.

2
  • The Thread.Sleep(5000); is for testing/demonstration only I hope? Jul 18, 2012 at 6:50
  • It's there so it doesn't update every time it has finished. I am using it because this grabs info from the servers. Is this a bad idea? Jul 18, 2012 at 6:52

3 Answers 3

4

this looks like an infinite recursion call, end condition missing?

public void invoke(MethodInvoker m) 
{
   try { invoke(m); } catch {} 
}
1
  • There is no 'end condition missing', this recursion is not serving any purpose. It should never start. Jul 18, 2012 at 7:48
2

You need to change it to:

public void invoke(MethodInvoker m) 
{
   try { someControl.Invoke(m); } catch {} 
}
3
  • I changed 'someControl' to 'list' (the name of my listview) it doesn't error now, but also doesn't do anything to the listview. Jul 18, 2012 at 6:58
  • 1
    Well, catch {} isn't very helpful in detecting what happens. Jul 18, 2012 at 7:00
  • Nevermind, it seems I was using the wrong method to clear the listview. Jul 18, 2012 at 7:02
1

Your invoke method is calling itself recursively with no termination condition. What you need to do is check whether the control requires that the operation be dispatched on the UI thread, and re-invoke the method appropriately.

You should use the Invoke method on some control that was created on the same thread (this could be the control which will be updated, or it could be the parent form; it doesn't really matter):

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

As a side note, though, it's very bad practice to have an empty umbrella catch block like that. In doing so you're essentially ignoring any possible error that might occur, whether or not it can/should be ignored. You should only attempt to catch exceptions that you know how to deal with; otherwise, let it propagate back up the stack to some code that can deal with it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.