Czeshirecat
Czeshirecat

Reputation: 546

control.invoke with an out parameter

Winforms, C#, VS2010.

I have a polling thread that runs for the lifetime of my app.

Occasionally it calls an event on my main form. I've not touched the code for years and it's run successfully but now I need to add an "out" parameter to the list of parameters. I've searched online but all the threads I've found have been regarding reflection and been complex to attempt to convert to my context. Mine doesn't use reflection.

Can somebody help over how to fix this pls? On the reflection threads I read people seem to check some object array for the out parameter result, which I don't use in my code, and I wouldn't know where to get it anyway.

private bool OnNeedUpdateCreateEvent(string title, string message,
  bool creatingNew, out string newPlanName)
{
    newPlanName = "";

    // 1st pass through this function. 
    // Check to see if this is being called from another thread rather 
    // than the main thread. If so then invoke is required
    if (InvokeRequired)
    {
      // Invoke and recall this method.
      return (bool)Invoke(new onNeedToUpdatePlanEvent(OnNeedUpdateCreateEvent),
        title, message, creatingNew, out newPlanName); <- wrong out param

    }
    else
    {
      // 2nd pass through this function due to invoke, or invoke not required
      return InputDlg(this, title, message, creatingNew, out newPlanName);
    }

} 

Upvotes: 5

Views: 944

Answers (1)

Hans Passant
Hans Passant

Reputation: 942255

It is much like you already know, you just haven't found the array yet. It is automatically created by the compiler. The signature of the Invoke method is:

public object Invoke(
    Delegate method,
    params object[] args
)

It is the params keyword that gets the compiler to auto-create the array. Nice syntax sugar, but it doesn't help you here. You just have to do it yourself, like this:

if (!creatingNew) {
    // Invoke and recall this method.
    object[] args = new object[] { title, message, creatingNew, null };
    var retval = (bool)Invoke(new onNeedToUpdatePlanEvent(OnNeedUpdateCreateEvent), args);
    newPlanName = (string)args[3];
    return retval;
}
// etc..

Upvotes: 5

Related Questions