I am trying to open a loading window while the else code keeps executing in the background, and close it whenever required (without threading).
Something like this:
LoadingWindow.ShowDialog();
Thread.Sleep(2000); //Simulates slowness
AnotherForm.ShowDialog();
LoadingWindow.Close(); //After AnotherForm displayed.
I can't use only LoadingWindow.Show();
to continue execution because LoadingWindow will not display correctly till the code after LoadingWindow.Show();
gets executed.
I have a custom Async ShowDialog method ShowDialogAsync();
, but the problem is await will not reach till the AnotherForm.ShowDialog();
get completed.
I tried:
var LoadingTask = LoadingWindow.ShowDialogAsync();
Thread.Sleep(2000); //Simulates slowness
//await AnotherForm.ShowDialogAsync(); //Not worked
//AnotherForm.ShowDialog(); //Not worked
//AnotherForm.Show(); //Not Worked
LoadingWindow.Close();
await LoadingTask;
This can only be used with await for simple methods:
var LoadingTask = LoadingWindow.ShowDialogAsync();
var data = await LoadDataAsync();
LoadingWindow.Close();
await LoadingTask;
//Sample simple method
private void LoadDataAsync()
{
await Task.Delay(2000);
return 10;
}
ShowDialogAsync:
public static async Task<DialogResult> ShowDialogAsync(this Form @this)
{
await Task.Yield();
if (@this.IsDisposed)
return DialogResult.OK;
return @this.ShowDialog();
}