具有方法参数值或使用委托的 c# 函数

c# function which has method parameter value, or using delegate

我的 window 表单应用程序中有两个按钮单击事件。

private void butProcess_1_Click(...)
{
    /// below parameters are just sample.
    Process_1(int_param1, decimal_param2, datetime_param3);
}

private void butProcess_2_Click(...)
{
    /// below parameters are just sample.
    Process_2(string_param1, guid_param2, byteArray_param3, bool_param4);
}

由于这些进程需要更长的时间才能完成,我需要向用户显示进度条。
所以我修改了名为 butProcess_1_Click.

的事件
private void butProcess_1_Click(...)
{
    frmLoadingControl _frmLoadingControl = new frmLoadingControl();            
    _frmLoadingControl.Show(this);

    BackgroundWorker _BackgroundWorker = new BackgroundWorker();
    _BackgroundWorker.DoWork += (s, args) =>
    {
        this.Invoke(new MethodInvoker(() => this.Enabled = false));
        /// below parameters are just sample.
        Process_1(int_param1, decimal_param2, datetime_param3);
    };
    _BackgroundWorker.RunWorkerCompleted += (s, args) =>
    {
        _frmLoadingControl.Close();
        this.Invoke(new MethodInvoker(() => this.Enabled = true));
    };

    _BackgroundWorker.RunWorkerAsync();

}

一切正常 correctly.But 问题是 butProcess_2_Click,我需要从 butProcess_1_Click 复制所有代码。 我只需更改一行即可调用 process_2().

Process_2(string_param1, guid_param2, byteArray_param3, bool_param4);

我不想重复我的代码。我想做的是像下面这样。

public void GenericFunction(Function _FunctionCode)
{
    frmLoadingControl _frmLoadingControl = new frmLoadingControl();            
    _frmLoadingControl.Show(this);

    BackgroundWorker _BackgroundWorker = new BackgroundWorker();
    _BackgroundWorker.DoWork += (s, args) =>
    {
        this.Invoke(new MethodInvoker(() => this.Enabled = false));
        /// below parameters are just sample.
        //Process_1(int_param1, decimal_param2, datetime_param3);
        //Process_2(string_param1, guid_param2, byteArray_param3, bool_param4);
        Execute(_FunctionCode);
    };
    _BackgroundWorker.RunWorkerCompleted += (s, args) =>
    {
        _frmLoadingControl.Close();
        this.Invoke(new MethodInvoker(() => this.Enabled = true));
    };

    _BackgroundWorker.RunWorkerAsync();
}

private void butProcess_1_Click(...)
{
    /// below parameters are just sample.
    //Process_1(int_param1, decimal_param2, datetime_param3);
    GenericFunction(Process_1(int_param1, decimal_param2, datetime_param3));
}

private void butProcess_2_Click(...)
{
    /// below parameters are just sample.
    //Process_2(string_param1, guid_param2, byteArray_param3, bool_param4);
    GenericFunction(Process_2(string_param1, guid_param2, byteArray_param3, bool_param4));
}

请让我得到你的建议。

传递委托,像这样:

// CHANGE HERE
public void GenericFunction(Action action)
{
    frmLoadingControl _frmLoadingControl = new frmLoadingControl();            
    _frmLoadingControl.Show(this);

    BackgroundWorker _BackgroundWorker = new BackgroundWorker();
    _BackgroundWorker.DoWork += (s, args) =>
    {
        this.Invoke(new MethodInvoker(() => this.Enabled = false));

        // CHANGE HERE
        action();

        Execute(_FunctionCode);
    };
    _BackgroundWorker.RunWorkerCompleted += (s, args) =>
    {
        _frmLoadingControl.Close();
        this.Invoke(new MethodInvoker(() => this.Enabled = true));
    };

    _BackgroundWorker.RunWorkerAsync();
}

private void butProcess_1_Click(...)
{
    // CHANGE HERE
    GenericFunction(() => Process_1(int_param1, decimal_param2, datetime_param3));
}

这个: _frmLoadingControl.Close();不应该放在下一行的Invoke吗?它是 "acts" 在 "piece" 的 Winforms 上...

this.Invoke(new MethodInvoker(() => 
{
    _frmLoadingControl.Close();
    this.Enabled = true;
}));

您应该使用 Action 委托 - https://msdn.microsoft.com/en-us/library/018hxwa8(v=vs.110).aspx

后台工作者已过时。相反,您应该使用异步方法。 - https://msdn.microsoft.com/en-us/library/hh191443.aspx