今天我注意到我的代碼中有一個錯誤,我想我以前處理過。我正在處理Windows Forms
應用程序,我經常使用form_load
事件。爲了處理這種情況時出現了錯誤的Form_load
事件和異常被拋出我使用下面的代碼:Windows窗體 - 如果form_load事件失敗,使用BeginInvoke
catch (Exception ex)
{
LogErrorAndShowMessage(ex, Resources.ERROR_LOAD);
//Otherwise Error : "Cannot call Close() while doing CreateHandle()".
this.BeginInvoke(new InvokeDelegate(CloseTheForm));
LoadForm<Materials>(closeAlreadyOpened : false);
}
這就是我如何聲明委託和方法:
public delegate void InvokeDelegate();//used if form load fails
private void CloseTheForm()
{
this.Close();
}
它的工作從今天起罰款。發生了什麼 - 在Form_load
事件中,我爲我的ComboBoxes設置了DataSource
,所以碰巧從SetDataSource
方法拋出了一個錯誤,這個錯誤最終與我在使用BeginInvoke
- Cannot call Close() while doing CreateHandle()
之前得到的錯誤完全相同。
這是例外的行爲,如果從其他地方拋出異常,代碼仍然工作正常,只有在設置數據源期間,我得到了錯誤。
任何建議可能導致這種異常行爲,我該如何解決?
P.S
這是我如何創造新的形式:
protected void LoadForm<T>(ToolStripButton/*ToolStripDropDownButton*/ formButton,
string buttonText,
long? loadEntityId = null,
bool closeAlreadyOpened = true) where T : BaseForm
{
MainForm parentFrm = GetParentForm();
if (parentFrm != null)
{
if (formButton != null)
{
parentFrm.SetForeColor(formButton);
}
T openForm = SingletonFormProvider.GetInstance<T>(parentFrm,
closeAlreadyOpened);
openForm.LoadEntityId = loadEntityId;
openForm.MdiParent = parentFrm;
openForm.Dock = DockStyle.Fill;
openForm.Show();
openForm.Activate();
}
else
{
MessageBox.Show("Parent form not found!", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
也許作爲提供我應該把try-catch
的openForm.Show()
各地處理這個問題Hans Passant
?
這可能是一個愚蠢的問題,但是您可以將代碼從Form_Load方法移動到窗體的構造函數嗎?如果表單沒有顯示/顯示,您將不會遇到處理錯誤... – schglurps 2013-03-26 17:58:40
我沒有真正想過這個。我不確定構造函數是否適合這個。我想這是可能的。 – Leron 2013-03-26 18:00:08