我的窗口表单应用程序中有两个按钮单击事件。
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();
}
一切正常。但问题是但是进程_2点击我需要从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));
}
请让我接受你的建议。
答案 0 :(得分: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));
}
这个:_frmLoadingControl.Close();
不应该放在下一行的Invoke
中吗?它是“作用”Winforms的“片段”......
this.Invoke(new MethodInvoker(() =>
{
_frmLoadingControl.Close();
this.Enabled = true;
}));
答案 1 :(得分:1)
您应该使用操作委托 - https://msdn.microsoft.com/en-us/library/018hxwa8(v=vs.110).aspx
后台工作人员已过时。相反,您应该使用异步方法。 - https://msdn.microsoft.com/en-us/library/hh191443.aspx