gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 19:57:23 25 4
gpt4 key购买 nike

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

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

}

一切正常。但问题是 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));
}

请让我得到你的建议。

最佳答案

像这样传递一个委托(delegate):

// 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 中吗?它是在 Winforms 的“片段”上“起作用”的东西......

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

关于具有方法参数值或使用委托(delegate)的 c# 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30253124/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com