c#函数,它有方法参数值,或者使用委托

Fra*_*Thu 2 c# delegates delegation

我的窗口表单应用程序中有两个按钮单击事件.

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);
}
Run Code Online (Sandbox Code Playgroud)

由于这些过程需要较长时间才能完成执行,因此我需要向用户显示进度条.
所以我修改了名为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();

}
Run Code Online (Sandbox Code Playgroud)

一切正常.但问题是但是进程_2点击我需要从butProcess_1_Click复制所有代码.我只需要更改一行来调用process_2().

Process_2(string_param1, guid_param2, byteArray_param3, bool_param4);
Run Code Online (Sandbox Code Playgroud)

我不想复制我的代码.我想做的就像下面.

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));
}
Run Code Online (Sandbox Code Playgroud)

请让我得到你的建议.

xan*_*tos 6

通过一个代表,像这样:

// 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));
}
Run Code Online (Sandbox Code Playgroud)

不应该这样:_frmLoadingControl.Close();放在Invoke下一行?它是"作用"Winforms"片段"的东西......

this.Invoke(new MethodInvoker(() => 
{
    _frmLoadingControl.Close();
    this.Enabled = true;
}));
Run Code Online (Sandbox Code Playgroud)