-
Notifications
You must be signed in to change notification settings - Fork 1
WinForm Display Busy Form after OnClick
DUONG Phu-Hiep edited this page Oct 17, 2018
·
3 revisions
internal static class DialogExt
{
public static async Task<DialogResult> ShowDialogAsync(this Form @this, Form parent)
{
await Task.Yield(); //force it to be async
if (@this.IsDisposed)
return DialogResult.OK;
return @this.ShowDialog(parent); //this will run on the UI thread at some point later
}
}
/// <summary>
/// Use the 'async' keyword
/// </summary>
private async void sendAsyncBf_Click(object sender, EventArgs e)
{
try
{
Result result = null;
using (var bf = new BusyForm())
{
var showBusyFormTask = bf.ShowDialogAsync(this);
result = await DoJobAsync();
bf.Close();
await showBusyFormTask;
}
log(JsonConvert.SerializeObject(result, Formatting.Indented));
}
catch (Exception ex)
{
log(ex.ToString());
}
}
/// <summary>
/// Without the 'async' keyword
/// </summary>
private void sendAsyncBf2_Click(object sender, EventArgs e)
{
try
{
using (var busyForm = new BusyForm())
{
busyForm.Shown += async (sd, args) =>
{
try
{
var result = await DoJobAsync();
log(JsonConvert.SerializeObject(result, Formatting.Indented));
}
catch (Exception ex)
{
log(ex.ToString());
}
finally
{
busyForm.Close();
}
};
busyForm.ShowDialog(this);
}
}
catch (Exception ex)
{
log(ex.ToString());
}
}