Skip to content

Instantly share code, notes, and snippets.

@brenoferreira
Created November 15, 2011 00:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brenoferreira/1365758 to your computer and use it in GitHub Desktop.
Save brenoferreira/1365758 to your computer and use it in GitHub Desktop.
Async Examples
using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Net;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AysncWinForms
{
public partial class Form1 : Form
{
private TextBox textBox1;
public Form1()
{
InitializeComponent();
this.ClientSize = new System.Drawing.Size(647, 454);
CreateTextBox();
Button b1 = new Button();
b1.Text = "Wait5scs";
b1.Click += async (sender, e) =>
{
Stopwatch sw = Stopwatch.StartNew();
await Task.Delay(TimeSpan.FromSeconds(5));
sw.Stop();
textBox1.Text = "Elapsed milliseconds: " + sw.ElapsedMilliseconds;
};
this.Controls.Add(b1);
Button b2 = new Button();
b2.Text = "CntTo10";
b2.Location = new Point(0, 50);
b2.Click += async (sender, e) =>
{
var count = await CountToTen();
textBox1.Text = count.ToString();
};
this.Controls.Add(b2);
Button b3 = new Button();
b3.Text = "Dwld Bing";
b3.Location = new Point(0, 100);
b3.Click += (sender, e) =>
{
var webRequest = WebRequest.Create("http://www.google.com");
webRequest.BeginGetResponse(new AsyncCallback(responseResult =>
{
var response = webRequest.EndGetResponse(responseResult);
StreamReader reader = new StreamReader(response.GetResponseStream());
textBox1.Text = reader.ReadLine();
}), null);
};
this.Controls.Add(b3);
Button b4 = new Button();
b4.Text = "DwldBingAsync";
b4.Location = new Point(0, 150);
b4.Click += async(sender, e) =>
{
var webRequest = WebRequest.Create("http://www.google.com");
var response = await webRequest.GetResponseAsync();
StreamReader reader = new StreamReader(response.GetResponseStream());
textBox1.Text = reader.ReadLine();
};
this.Controls.Add(b4);
Button b5 = new Button();
b5.Text = "DeadLock";
b5.Location = new Point(0, 200);
b5.Click += (sender, e) =>
{
var t = CountToTen();
t.Wait();
textBox1.Text = "Delayed 5 seconds";
};
this.Controls.Add(b5);
Button b6 = new Button();
b6.Text = "NoDeadLock";
b6.Location = new Point(0, 250);
b6.Click += (sender, e) =>
{
var t = CountToTenWithoutSyncContext();
t.Wait();
textBox1.Text = "Delayed 5 seconds";
};
this.Controls.Add(b6);
Button b7 = new Button();
b7.Text = "AsyncImpl";
b7.Location = new Point(0, 300);
b7.Click += async (sender, e) =>
{
var str = await this.DownloadStringAsync("http://db.gamefaqs.com/computer/doswin/file/fallout_new_vegas_a.txt");
textBox1.Text = str;
};
this.Controls.Add(b7);
}
private void CreateTextBox()
{
this.textBox1 = new System.Windows.Forms.TextBox();
//
// textBox1
//
this.textBox1.Dock = System.Windows.Forms.DockStyle.Right;
this.textBox1.Location = new System.Drawing.Point(311, 0);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(336, 454);
this.textBox1.TabIndex = 0;
this.Controls.Add(this.textBox1);
}
public async Task<int> CountToTenWithoutSyncContext()
{
int count = 1;
for (int i = 0; i < 10; i++)
{
var t = Task.Delay(500);
await t.ConfigureAwait(false);
count += i;
}
return count;
}
public async Task<int> CountToTen()
{
int count = 1;
for (int i = 0; i < 10; i++)
{
await Task.Delay(500);
count += i;
}
return count;
}
public void PrintCountToTen()
{
var task = CountToTen();
System.Runtime.CompilerServices.TaskAwaiter<int> taskAwaiter = task.GetAwaiter();
taskAwaiter.OnCompleted(() => Console.WriteLine(taskAwaiter.GetResult()));
}
private Task<String> DownloadStringAsync(string uri)
{
var webclient = new WebClient();
var downloadTaskCompletionSource = new TaskCompletionSource<String>();
var downloadTask = downloadTaskCompletionSource.Task;
webclient.DownloadStringCompleted += (sender, e) =>
{
downloadTaskCompletionSource.SetResult(e.Result);
};
webclient.DownloadStringAsync( new Uri(uri));
return downloadTask;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment