我在窗口窗體應用程序中有兩個按鈕單擊事件。具有方法參數值或使用委託的c#函數
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));
}
請讓我得到您的建議。