The right way to handle this is to cancel the event Close, and then close the form for real when the task completes. It might look something like this:
private async void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
cancelUpdater.Cancel();
if (!updater.IsCompleted)
{
this.Hide();
e.Cancel = true;
await updater;
this.Close();
}
}
Now in your comments you wrote:
the Close () method called by form B will immediately return and the changes that form B will cause the update to fail
- , " B", , - Form1. , " B", Form1 . , , , , , , .
, UI. , , - . , , . , , .
, .
, " B" , , BeginInvoke() Invoke() ( , " " , ). , , , , . .
, , , , , , System.Windows.Forms.Timer. , .
:
Timer . timer1. Interval 1000 , . , Updater() , timer1_Tick(object sender, EventArgs e) Tick.
, :
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
timer1.Stop();
}
private void Form1_Shown(object sender, EventArgs e)
{
timer1.Start();
}
void timer1_Tick(object sender, EventArgs e)
{
...
}
System.Windows.Forms.Timer Tick , . FormClosing, . . , , Tick , Invoke() .
, - , . , , , , .