可能重複:
How to stop BackgroundWorker on Form's Closing event?Windows窗體 - 調用Control.Invoke是給錯誤提示控制尚未加載
我有一個Windows窗體一些代碼有點像這個:
button1_Click(object sender, EventArgs e)
{
Widget myWidget = new Widget(...bunch of constructor parameters...);
myWidget.Property1 = "blah"; // other properties get set here too...
myWidget.InterestingThingHappened += InterestingThingHappened;
Parallel.Invoke(myWidget.RunInterestingLongRunningProcess());
}
private void InterestingThingHappened(object sender, EventArgs e)
{
myLabel.Invoke(new Action(() => myLabel.Text = "An interesting thing happened!"))
}
當我運行它,應用程序只是凍結,直到我在任務管理器中殺死它。然後我看到一個異常說'調用或BeginInvoke不能被調用,直到窗口句柄被創建。 button1_Click
顯然是一個控件事件處理程序,因此表單在此代碼運行之前完全創建。我完全不知道這意味着什麼。誰能幫忙?
當谷歌搜索這個問題時,我永遠不會認識到你發佈的問題與我的相同。它仍然沒有看到它是... – David
這絕不意味着鏈接問題的重複。我的代碼不使用BackgroundWorker。我並不想關閉表格。我想知道是否有辦法對此提出申訴? – David