How to use Reflection to Invoke an Overloaded Method in .NET

You have to specify which method you want: class SomeType { void Foo(int size, string bar) { } void Foo() { } } SomeType obj = new SomeType(); // call with int and string arguments obj.GetType() .GetMethod(“Foo”, new Type[] { typeof(int), typeof(string) }) .Invoke(obj, new object[] { 42, “Hello” }); // call without arguments obj.GetType() … Read more

Avoid calling Invoke when the control is disposed

What you have here is a race condition. You’re better off just catching the ObjectDisposed exception and be done with it. In fact, I think in this case it is the only working solution. try { if (mImageListView.InvokeRequired) mImageListView.Invoke(new YourDelegate(thisMethod)); else mImageListView.RefreshInternal(); } catch (ObjectDisposedException ex) { // Do something clever }

Invoke in Windows Forms

Did you try MSDN Control.Invoke I just wrote a little WinForm application to demonstrate Control.Invoke. When the form is created, Start some work on background thread. After that work is done, Update the status in a label. public Form1() { InitializeComponent(); //Do some work on a new thread Thread backgroundThread = new Thread(BackgroundWork); backgroundThread.Start(); } … Read more

Is it appropriate to extend Control to provide consistently safe Invoke/BeginInvoke functionality?

You should create Begin and End extension methods as well. And if you use generics, you can make the call look a little nicer. public static class ControlExtensions { public static void InvokeEx<T>(this T @this, Action<T> action) where T : Control { if (@this.InvokeRequired) { @this.Invoke(action, new object[] { @this }); } else { if … Read more