匹配委托“System.Action”没有重载

Mar*_*rin 0 c# action

public void UpdateDataGrid(bool newInsert = false)
    {

        //ThreadSafe (updating datagridview from AddEventForm is not allowed otherwise 
        if (InvokeRequired)
        {
            Invoke(new Action(UpdateDataGrid));
        }
        else
        {
            Util.PopulateDataGridView(ref this.EventsDataGridView,newInsert);
        }
    }
Run Code Online (Sandbox Code Playgroud)

我不知道如何为 new Action() 提供可选参数。

我尝试了 new Action(UpdateDataGrid) 但仍然引发运行时错误。

谢谢

Dou*_*las 6

您需要创建一个方法委托来封装您的方法调用,传递最初指定的参数,如下所示:

() => UpdateDataGrid(newInsert)
Run Code Online (Sandbox Code Playgroud)

在上下文中:

public void UpdateDataGrid(bool newInsert = false)
{

    //ThreadSafe (updating datagridview from AddEventForm is not allowed otherwise 
    if (InvokeRequired)
    {
        Invoke(new Action(() => UpdateDataGrid(newInsert)));
    }
    else
    {
        Util.PopulateDataGridView(ref this.EventsDataGridView,newInsert);
    }
}    
Run Code Online (Sandbox Code Playgroud)