设置:带进度条和标签的主MDI表单。Winforms更新用户界面异步模式 - 需要概括
代码主要形式。
public delegate void UpdateMainProgressDelegate(string message, bool isProgressBarStopped);
private void UpdateMainProgress(string message, bool isProgressBarStopped)
{
// make sure we are running on the right thread to be
// updating this form's controls.
if (InvokeRequired == false)
{
// we are running on the right thread. Have your way with me!
bsStatusMessage.Caption = message + " [ " + System.DateTime.Now.ToShortTimeString() + " ]";
progressBarStatus.Stopped = isProgressBarStopped;
}
else
{
// we are running on the wrong thread.
// Transfer control to the correct thread!
Invoke(new ApplicationLevelValues.UpdateMainProgressDelegate(UpdateMainProgress), message, isProgressBarStopped);
}
}
子窗体
private readonly ApplicationLevelValues.UpdateMainProgressDelegate _UpdateMainForm;
private void btnX_Click(object sender, EventArgs e)
{
_UpdateMainForm.BeginInvoke("StartA", false, null, null);
try
{
if(UpdateOperationA())
{ _UpdateMainForm.BeginInvoke("CompletedA", true, null, null); }
else
{ _UpdateMainForm.BeginInvoke("CanceledA", true, null, null); }
}
catch (System.Exception ex)
{
_UpdateMainForm.BeginInvoke("ErrorA", true, null, null);
throw ex;
}
}
其工作相当不错,但对于N按钮或N操作我不得不一次又一次地写相同的代码。有没有什么办法可以推广或更新用户界面的其他更好的方法。
这似乎好多了,我会实施这种方法,并更新如果我什么都疯狂!! ..谢谢。 – 2010-09-05 20:11:20