Possible Duplicate:
How to stop BackgroundWorker on Form's Closing event?
I have some code in a Windows Form a bit like this:
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!"))
}
When I run it, the application just freezes until I kill it in Task Manager. Then I see an Exception saying 'Invoke or BeginInvoke cannot be called on a control until the window handle has been created.' button1_Click
is clearly a control event handler, so the form is fully created before this code runs. I have absolutely no idea what this means. Can anyone help?