Last active
December 5, 2017 04:32
-
-
Save jigargandhi/75fe44b9aef222dac702e63c98c9c567 to your computer and use it in GitHub Desktop.
Show Modal Dialog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using System.Windows.Forms; | |
public partial class Form1 : Form | |
{ | |
public Form1() | |
{ | |
InitializeComponent(); | |
} | |
private async void button1_Click(object sender, EventArgs e) | |
{ | |
Task<int> waitTask = LongRunningTask(); | |
using (WaitDialog dialog = new WaitDialog(waitTask)) | |
{ | |
// UI will be blocked on this ShowDialog until task finishes | |
dialog.ShowDialog(this); | |
int result = await waitTask; | |
MessageBox.Show($"Result: {result}"); | |
} | |
} | |
public Task<int> LongRunningTask() | |
{ | |
return Task.Run(() => | |
{ | |
Thread.Sleep(10000); | |
return 10; | |
}); | |
} | |
/// <summary> | |
/// Required designer variable. | |
/// </summary> | |
private System.ComponentModel.IContainer components = null; | |
/// <summary> | |
/// Clean up any resources being used. | |
/// </summary> | |
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> | |
protected override void Dispose(bool disposing) | |
{ | |
if (disposing && (components != null)) | |
{ | |
components.Dispose(); | |
} | |
base.Dispose(disposing); | |
} | |
#region Windows Form Designer generated code | |
/// <summary> | |
/// Required method for Designer support - do not modify | |
/// the contents of this method with the code editor. | |
/// </summary> | |
private void InitializeComponent() | |
{ | |
this.button1 = new System.Windows.Forms.Button(); | |
this.SuspendLayout(); | |
// | |
// button1 | |
// | |
this.button1.Location = new System.Drawing.Point(66, 43); | |
this.button1.Name = "button1"; | |
this.button1.Size = new System.Drawing.Size(75, 23); | |
this.button1.TabIndex = 0; | |
this.button1.Text = "button1"; | |
this.button1.UseVisualStyleBackColor = true; | |
this.button1.Click += new System.EventHandler(this.button1_Click); | |
// | |
// Form1 | |
// | |
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); | |
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; | |
this.ClientSize = new System.Drawing.Size(284, 261); | |
this.Controls.Add(this.button1); | |
this.Name = "Form1"; | |
this.Text = "Form1"; | |
this.ResumeLayout(false); | |
} | |
#endregion | |
private System.Windows.Forms.Button button1; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Windows.Forms; | |
static class Program | |
{ | |
/// <summary> | |
/// The main entry point for the application. | |
/// </summary> | |
[STAThread] | |
static void Main() | |
{ | |
Application.EnableVisualStyles(); | |
Application.SetCompatibleTextRenderingDefault(false); | |
Application.Run(new Form1()); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Threading.Tasks; | |
using System.Windows.Forms; | |
public partial class WaitDialog : Form | |
{ | |
public WaitDialog(Task waitTask) | |
{ | |
InitializeComponent(); | |
waitTask.ContinueWith(t => | |
{ | |
this.DialogResult = DialogResult.OK; | |
}); | |
} | |
private Label label1; | |
/// <summary> | |
/// Required designer variable. | |
/// </summary> | |
private System.ComponentModel.IContainer components = null; | |
/// <summary> | |
/// Clean up any resources being used. | |
/// </summary> | |
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> | |
protected override void Dispose(bool disposing) | |
{ | |
if (disposing && (components != null)) | |
{ | |
components.Dispose(); | |
} | |
base.Dispose(disposing); | |
} | |
#region Windows Form Designer generated code | |
/// <summary> | |
/// Required method for Designer support - do not modify | |
/// the contents of this method with the code editor. | |
/// </summary> | |
private void InitializeComponent() | |
{ | |
this.label1 = new System.Windows.Forms.Label(); | |
this.SuspendLayout(); | |
// | |
// label1 | |
// | |
this.label1.AutoSize = true; | |
this.label1.Location = new System.Drawing.Point(39, 97); | |
this.label1.Name = "label1"; | |
this.label1.Size = new System.Drawing.Size(218, 13); | |
this.label1.TabIndex = 0; | |
this.label1.Text = "Please wait for long running task to complete"; | |
// | |
// WaitDialog | |
// | |
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); | |
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; | |
this.ClientSize = new System.Drawing.Size(284, 261); | |
this.Controls.Add(this.label1); | |
this.Name = "WaitDialog"; | |
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent; | |
this.Text = "WaitDialog"; | |
this.ResumeLayout(false); | |
this.PerformLayout(); | |
} | |
#endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment