2017-01-26 5 views
4

Winforms、C#、VS2010。outパラメータを持つcontrol.invoke

私は自分のアプリの生涯にわたって実行されるポーリングスレッドを持っています。

時折、私のメインフォームでイベントを呼び出します。私は何年もコードに触れていないし、正常に実行されましたが、今はパラメータのリストに "out"パラメータを追加する必要があります。私はオンラインで検索しましたが、私が見つけたすべてのスレッドは、リフレクションに関するもので、私のコンテキストに変換しようとすると複雑でした。私は反射を使用しません。

誰かがこのplsを修正する方法を手伝ってもらえますか?リフレクションスレッドでは、人々は、私のコードで使用していないoutパラメータ結果のためにオブジェクト配列をチェックするように見えますが、とにかくそれを取得する場所はわかりません。

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); 
    } 

} 

答えて

4

あなたがすでに知っているように、あなたはまだ配列を見つけていません。コンパイラによって自動的に作成されます。 Invoke methodのシグネチャは次のとおりです。

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

それは、アレイを自動作成するようにコンパイラを取得しparamsキーワードです。いい構文の砂糖、しかしここであなたを助けることはありません。あなたはちょうどそれをこのようにしなければなりません:

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.. 
関連する問題