Skip to content

Instantly share code, notes, and snippets.

@jkells
Last active August 29, 2015 14:10
Show Gist options
  • Select an option

  • Save jkells/4ec8e1b49597f305116d to your computer and use it in GitHub Desktop.

Select an option

Save jkells/4ec8e1b49597f305116d to your computer and use it in GitHub Desktop.
using System.Windows.Forms;
using AsyncWpf;
namespace AsyncWinForms
{
public partial class MainForm : Form
{
private readonly ExampleDatabaseService _databaseService = new ExampleDatabaseService();
private readonly ExamplePersonService _personService = new ExamplePersonService();
private readonly ExampleWebService _webService = new ExampleWebService();
public MainForm()
{
InitializeComponent();
}
}
}
namespace AsyncWinForms
{
partial class MainForm
{
/// <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.progressBar = new System.Windows.Forms.ProgressBar();
this.messageBox = new System.Windows.Forms.TextBox();
this.runSynchronousButton = new System.Windows.Forms.Button();
this.runAsyncTaskButton = new System.Windows.Forms.Button();
this.backgroundWorkerButton = new System.Windows.Forms.Button();
this.runAsyncLegacy = new System.Windows.Forms.Button();
this.runTaskButton = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// progressBar
//
this.progressBar.Location = new System.Drawing.Point(12, 42);
this.progressBar.Name = "progressBar";
this.progressBar.Size = new System.Drawing.Size(444, 23);
this.progressBar.TabIndex = 0;
//
// messageBox
//
this.messageBox.Location = new System.Drawing.Point(12, 16);
this.messageBox.Name = "messageBox";
this.messageBox.Size = new System.Drawing.Size(444, 20);
this.messageBox.TabIndex = 1;
//
// runSynchronousButton
//
this.runSynchronousButton.Location = new System.Drawing.Point(293, 326);
this.runSynchronousButton.Name = "runSynchronousButton";
this.runSynchronousButton.Size = new System.Drawing.Size(163, 23);
this.runSynchronousButton.TabIndex = 2;
this.runSynchronousButton.Text = "Run Synchronous";
this.runSynchronousButton.UseVisualStyleBackColor = true;
this.runSynchronousButton.Click += new System.EventHandler(this.RunSynchronousClick);
//
// runAsyncTaskButton
//
this.runAsyncTaskButton.Location = new System.Drawing.Point(293, 210);
this.runAsyncTaskButton.Name = "runAsyncTaskButton";
this.runAsyncTaskButton.Size = new System.Drawing.Size(163, 23);
this.runAsyncTaskButton.TabIndex = 3;
this.runAsyncTaskButton.Text = "Run Async Task";
this.runAsyncTaskButton.UseVisualStyleBackColor = true;
this.runAsyncTaskButton.Click += new System.EventHandler(this.RunAsyncTaskButtonClick);
//
// backgroundWorkerButton
//
this.backgroundWorkerButton.Location = new System.Drawing.Point(293, 297);
this.backgroundWorkerButton.Name = "backgroundWorkerButton";
this.backgroundWorkerButton.Size = new System.Drawing.Size(163, 23);
this.backgroundWorkerButton.TabIndex = 4;
this.backgroundWorkerButton.Text = "Run Background Worker";
this.backgroundWorkerButton.UseVisualStyleBackColor = true;
this.backgroundWorkerButton.Click += new System.EventHandler(this.BackgroundWorkerButtonClick);
//
// runAsyncLegacy
//
this.runAsyncLegacy.Location = new System.Drawing.Point(293, 268);
this.runAsyncLegacy.Name = "runAsyncLegacy";
this.runAsyncLegacy.Size = new System.Drawing.Size(163, 23);
this.runAsyncLegacy.TabIndex = 5;
this.runAsyncLegacy.Text = "Run Async Legacy";
this.runAsyncLegacy.UseVisualStyleBackColor = true;
this.runAsyncLegacy.Click += new System.EventHandler(this.RunAsyncLegacyClick);
//
// runTaskButton
//
this.runTaskButton.Location = new System.Drawing.Point(293, 239);
this.runTaskButton.Name = "runTaskButton";
this.runTaskButton.Size = new System.Drawing.Size(163, 23);
this.runTaskButton.TabIndex = 6;
this.runTaskButton.Text = "Run Task";
this.runTaskButton.UseVisualStyleBackColor = true;
this.runTaskButton.Click += new System.EventHandler(this.RunTaskButtonClick);
//
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(468, 361);
this.Controls.Add(this.runTaskButton);
this.Controls.Add(this.runAsyncLegacy);
this.Controls.Add(this.backgroundWorkerButton);
this.Controls.Add(this.runAsyncTaskButton);
this.Controls.Add(this.runSynchronousButton);
this.Controls.Add(this.messageBox);
this.Controls.Add(this.progressBar);
this.Name = "MainForm";
this.Text = "Example";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.ProgressBar progressBar;
private System.Windows.Forms.TextBox messageBox;
private System.Windows.Forms.Button runSynchronousButton;
private System.Windows.Forms.Button runAsyncTaskButton;
private System.Windows.Forms.Button backgroundWorkerButton;
private System.Windows.Forms.Button runAsyncLegacy;
private System.Windows.Forms.Button runTaskButton;
}
}
using System;
using System.Threading;
using System.Windows.Forms;
using AsyncWpf;
namespace AsyncWinForms
{
public partial class MainForm : Form
{
private void RunAsyncLegacyClick(object sender, EventArgs e)
{
var state = new State
{
Context = SynchronizationContext.Current
};
_personService.BeginGet("jkells@gmail.com", OnGetUserComplete, state);
}
private void OnGetUserComplete(IAsyncResult ar)
{
var state = (State) ar.AsyncState;
state.Person = _personService.EndGet(ar);
_webService.BeginGetBalance("jkells@gmail.com", OnGetBalanceComplete, state);
}
private void OnGetBalanceComplete(IAsyncResult ar)
{
var state = (State) ar.AsyncState;
state.Balance = _webService.EndGetBalance(ar);
state.Context.Post(o => { progressBar.Value = 50; }, null);
_databaseService.BeginSave(new DatabaseRecord
{
Balance = state.Balance,
Person = state.Person,
Timestamp = DateTime.UtcNow
}, OnSaveComplete, state);
}
private void OnSaveComplete(IAsyncResult ar)
{
var state = (State) ar.AsyncState;
state.Context.Post(o =>
{
progressBar.Value = 100;
messageBox.Text = "Complete";
}, null);
}
private class State
{
public SynchronizationContext Context { get; set; }
public Person Person { get; set; }
public decimal Balance { get; set; }
}
}
}
using System;
using System.Windows.Forms;
using AsyncWpf;
namespace AsyncWinForms
{
public partial class MainForm : Form
{
private async void RunAsyncTaskButtonClick(object sender, EventArgs e)
{
progressBar.Value = 10;
var person = await _personService.GetAsync("jkells@gmail.com");
progressBar.Value = 40;
var balance = await _webService.GetBalanceAsync("jkells@gmail.com");
progressBar.Value = 80;
await _databaseService.SaveAsync(new DatabaseRecord
{
Balance = balance,
Person = person,
Timestamp = DateTime.UtcNow
});
progressBar.Value = 100;
messageBox.Text = "Complete";
}
}
}
using System;
using System.ComponentModel;
using System.Windows.Forms;
using AsyncWpf;
namespace AsyncWinForms
{
public partial class MainForm : Form
{
private void RunSynchronousClick(object sender, EventArgs e)
{
var person = _personService.Get("jkells@gmail.com");
progressBar.Value = 50;
var balance = _webService.GetBalance("jkells@gmail.com");
_databaseService.Save(new DatabaseRecord
{
Balance = balance,
Person = person,
Timestamp = DateTime.UtcNow
});
progressBar.Value = 100;
messageBox.Text = "Complete";
}
}
}
using System;
using System.Windows.Forms;
using AsyncWpf;
namespace AsyncWinForms
{
public partial class MainForm : Form
{
private void RunTaskButtonClick(object sender, EventArgs e)
{
progressBar.Value = 10;
_personService.GetAsync("jkells@gmail.com")
.ContinueWith(task1 =>
{
_webService.GetBalanceAsync("jkells@gmail.com")
.ContinueWith(task2 =>
{
progressBar.Value = 50;
var task3 = _databaseService.SaveAsync(new DatabaseRecord
{
Balance = task2.Result,
Person = task1.Result,
Timestamp =
DateTime.UtcNow
});
task3.ContinueWith(t4 =>
{
progressBar.Value = 100;
messageBox.Text = "Complete";
});
});
});
}
}
}
using System;
using System.ComponentModel;
using System.Windows.Forms;
using AsyncWpf;
namespace AsyncWinForms
{
public partial class MainForm : Form
{
private void BackgroundWorkerButtonClick(object sender, System.EventArgs e)
{
var worker = new BackgroundWorker();
worker.DoWork += OnDoWork;
worker.ProgressChanged += OnProgressChanged;
worker.RunWorkerCompleted += OnWorkerCompleted;
worker.WorkerReportsProgress = true;
worker.RunWorkerAsync();
}
private void OnWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
var result = (Tuple<Person, decimal>) e.Result;
_databaseService.Save(new DatabaseRecord
{
Balance = result.Item2,
Person = result.Item1,
Timestamp = DateTime.UtcNow
});
progressBar.Value = 100;
}
private void OnProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar.Value = e.ProgressPercentage;
}
private void OnDoWork(object sender, DoWorkEventArgs e)
{
var worker = (BackgroundWorker) sender;
worker.ReportProgress(10);
var person = _personService.Get("jkells@gmail.com");
var balance = _webService.GetBalance("jkells@gmail.com");
e.Result = new Tuple<Person, decimal>(person, balance);
worker.ReportProgress(50);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment