Martin, You won't be able to invoke a method with parameters if you are using the MethodInvoker
delegate. Here is what MSDN says about MethodInvoker
: "MethodInvoker provides a simple delegate that is used to invoke a method with a void parameter list. This delegate can be used when making calls to a control's invoke method, or when you need a simple delegate but don't want to define one yourself." Since MethodInvoker
has a void parameter list, and since the function you specify must have the same parameters as the delegate in question, you won't be able to invoke a method with parameters using it. In order to do this, you need to define your own delegate, which has the correct argument list. Let's suppose that the function you want to invoke by BeginInvoke
has two parameters, a bool
and a String
, and that it looks like this:
public void ShowMyDialogBox(bool bTest, String message)
Now, you would define your delegate like this:
public delegate void MyDelegate(bool bTest, String message)
The name of the delegate is completely optional, as are the names of the parameters. We could have called the boolean parameter anything we want (e.g. bBoolParam
) and we could have called the string parameter anything we want. Now we would invoke this as follows:
BeginInvoke(new MyDelegate(ShowMyDialogBox), new object [] { varToBeParam1, varToBeParam2 });
So now, if we put that together, our ShowMyDialogBox
function would look something like this (assuming the scenario which McSmack had laid out):
public void ShowMyDialogBox(bool bTest, String message)
{
if(this.InvokeRequired)
{
this.BeginInvoke(new this.MyDelegate(this.ShowMyDialogBox), new object [] { bTest, message });
return;
}
// now we're on the main thread and can do processing as normal
// the parameters also have their proper values
...
}
Hope that helps! Feel free to respond if you have any more questions about this, or if you do not understand something in the answer. Sincerely, Alexander Wiseman