0

I'm trying to modify a combo box on my WinForms application, and I'm getting some strange behavior. I'm trying two methods:

Here is the method I need to invoke:

private void modifyCombo(ClassInfoHolder oldClass, ClassInfoHolder newClass) {
    this.monitoredComboBox.Items[monitoredComboBox.Items.IndexOf(oldClass)] = newClass;
}

I'm trying two different ways to invoke this method from the GUI thread. This one works:

delegate void modifyComboCollection(ClassInfoHolder oldClass, ClassInfoHolder newClass);

private void modifySecondTabComboBox(ClassInfoHolder oldClass, ClassInfoHolder newClass) {
    if (monitoredComboBox.InvokeRequired) {
        modifyComboCollection m = new modifyComboCollection(modifyCombo);
        this.BeginInvoke(m, oldClass, newClass);
    } else {
        // no need for Invoke
        modifyCombo(oldClass, newClass);
    }
}

And this throws a TargetInvocationException:

this.BeginInvoke(new Action(() => {
    modifyCombo(oldClass, newClass);
}));

I'd prefer to use the second because it's much clearer, but I'm not entirely sure why it throws an error when the first example works just fine. The first example calls the modifyCombo method and correctly returns the IndexOf of the object. The second example is returned -1 from IndexOf.

Edit: Here is a pastebin link of the stacktrace. http://pastebin.com/TwfUDw4u

Corey
  • 1,177
  • 4
  • 16
  • 24
  • 3
    Can you paste the full stack trace of the exception? TargetInvocationException usually indicates that some exception was thrown elsewhere, and it was wrapped. Knowing the real exception and where it happened will help debug. – cdhowie Nov 24 '10 at 17:07
  • @cdhowie Added; I'm a little baffled by it. – Corey Nov 24 '10 at 17:15
  • @SLaks Sorry about that, I just updated the link right before your post. The InnerException is that the `indexOf` method is returning `-1`, but I have no idea why because the combo box is populated and most definitely does have the `oldClass` object inside of it. – Corey Nov 24 '10 at 17:20
  • BeginInvoke with 3 arguments? I'm confused – Roman Pokrovskij Nov 24 '10 at 17:28
  • The first argument is the method to call, second argument for BeginInvoke is `params object[] args`, for all the parameters the method takes – Corey Nov 24 '10 at 17:31
  • Maybe use a debugger to see why the IndexOutOfRangeException is being thrown? – cdhowie Nov 24 '10 at 17:31
  • oh nevermind, in my last comment I was iterating through and modifying the collection, which isn't a good idea. I fully understand why I get an error there. The problem in the main question still persists though -- I have NO idea why it is saying that my object does not exist in the collection when it does, and I'm not modifying the collection while iterating through it in any way... the two use cases are identical but one works while the other fails – Corey Nov 24 '10 at 17:49
  • Note about the Invoke() method (from MSDN): A call to an EventHandler or MethodInvoker delegate will be faster than a call to another type of delegate. – Bryan Nov 24 '10 at 20:33
  • From that stack trace it sure seems like the problem is in the modifyCombo() method and not with the other stuff. Looks like you should be checking the index before using it. – Bryan Nov 24 '10 at 20:34

1 Answers1

0

this.BeginInvoke(m, new[] {oldClass, newClass});

BTW. Good practice is to test if (this.IsHandleCreated && !this.IsDisposed) before use Invoke.

Roman Pokrovskij
  • 9,449
  • 21
  • 87
  • 142