Skip to content

Instantly share code, notes, and snippets.

@nilaydshah
Created October 5, 2015 12:35
Show Gist options
  • Save nilaydshah/6cd2e7692047d9061d94 to your computer and use it in GitHub Desktop.
Save nilaydshah/6cd2e7692047d9061d94 to your computer and use it in GitHub Desktop.
Async Method in C#, using async and await
using System;
using System.IO;
using System.Threading.Tasks;
class AsyncAwaitImpl
{
static void Main()
{
// Create task and start it.
// ... Wait for it to complete.
Task task = new Task(ProcessDataAsync);
task.Start();
task.Wait();
Console.ReadLine();
}
static async void ProcessDataAsync()
{
// Start the HandleFile method.
Task<int> task = HandleFileAsync("C:\\enable1.txt");
// Control returns here before HandleFileAsync returns.
// ... Prompt the user.
Console.WriteLine("Please wait patiently " +
"while I do something important.");
// Wait for the HandleFile task to complete.
// ... Display its results.
int x = await task;
Console.WriteLine("Count: " + x);
}
static async Task<int> HandleFileAsync(string file)
{
Console.WriteLine("HandleFile enter");
int count = 0;
// Read in the specified file.
// ... Use async StreamReader method.
using (StreamReader reader = new StreamReader(file))
{
string v = await reader.ReadToEndAsync();
// ... Process the file data somehow.
count += v.Length;
// ... A slow-running computation.
// Dummy code.
for (int i = 0; i < 10000; i++)
{
int x = v.GetHashCode();
if (x == 0)
{
count--;
}
}
}
Console.WriteLine("HandleFile exit");
return count;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment