No overload for matches delegate 'System.Actio

2019-09-18 13:45发布

问题:

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

I don't know how to provide the optional parameter to new Action().

I tried new Action(UpdateDataGrid) but still throws a runtime error.

Thanks

回答1:

You need to create a method delegate encapsulating the invocation of your method, passing the argument originally specified, like so:

() => UpdateDataGrid(newInsert)

In context:

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


标签: c# action