Last active
January 27, 2016 09:26
-
-
Save sebingel/32696af96f0bda4acafb to your computer and use it in GitHub Desktop.
A small application to demonstrate different implementations of parallel computation in c# featuring await async pattern, BackgroundWorker and Task.
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.ComponentModel; | |
using System.IO; | |
using System.Threading.Tasks; | |
namespace sebingel.ParallelComparision | |
{ | |
internal class Program | |
{ | |
static void Main() | |
{ | |
// Instant return | |
Task.Factory.StartNew(ViaTask.ProcessDataAsync); | |
ViaAwaitAsync.ProcessDataAsync(); | |
ViaBackgroundWorker.ProcessDataAsync(); | |
Console.ReadLine(); | |
} | |
} | |
internal static class ViaAwaitAsync | |
{ | |
internal static async void ProcessDataAsync() | |
{ | |
// Start the time intensive method | |
Task<int> task = TimeintensiveMethod(@"PATH_TO_SOME_FILE"); | |
// Control returns here before TimeintensiveMethod returns | |
Console.WriteLine("You can read this while TimeintensiveMethod is still running."); | |
// Wait for the TimeintensiveMethod task to complete | |
int x = await task; | |
Console.WriteLine("Count: " + x); | |
} | |
private static async Task<int> TimeintensiveMethod(string file) | |
{ | |
Console.WriteLine("Start TimeintensiveMethod."); | |
// Do some time intensive calculations... | |
using (StreamReader reader = new StreamReader(file)) | |
{ | |
string v = await reader.ReadToEndAsync(); | |
for (int i = 0; i < 10000; i++) | |
v.GetHashCode(); | |
} | |
Console.WriteLine("End TimeintensiveMethod."); | |
// return something to demonstrate the coolness of await-async | |
return new Random().Next(100); | |
} | |
} | |
internal static class ViaBackgroundWorker | |
{ | |
internal static void ProcessDataAsync() | |
{ | |
// Start the time intensive method | |
BackgroundWorker bw = new BackgroundWorker(); | |
bw.DoWork += BwDoWork; | |
bw.RunWorkerCompleted += BwRunWorkerCompleted; | |
bw.RunWorkerAsync(@"PATH_TO_SOME_FILE"); | |
// Control returns here before TimeintensiveMethod returns | |
Console.WriteLine("You can read this while TimeintensiveMethod is still running."); | |
} | |
static void BwRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) | |
{ | |
Console.WriteLine("Count: " + e.Result); | |
} | |
static void BwDoWork(object sender, DoWorkEventArgs e) | |
{ | |
e.Result = TimeintensiveMethod(e.Argument); | |
} | |
private static int TimeintensiveMethod(object file) | |
{ | |
Console.WriteLine("Start TimeintensiveMethod."); | |
// Do some time intensive calculations... | |
using (StreamReader reader = new StreamReader(file.ToString())) | |
{ | |
string v = reader.ReadToEnd(); | |
for (int i = 0; i < 10000; i++) | |
v.GetHashCode(); | |
} | |
Console.WriteLine("End TimeintensiveMethod."); | |
return new Random().Next(100); | |
} | |
} | |
internal static class ViaTask | |
{ | |
internal static void ProcessDataAsync() | |
{ | |
// Start the time intensive method | |
Task<int> t = Task<int>.Factory.StartNew(TimeintensiveMethod, @"PATH_TO_SOME_FILE"); | |
// Control returns here before TimeintensiveMethod returns | |
Console.WriteLine("You can read this while TimeintensiveMethod is still running."); | |
Console.WriteLine("Count: " + t.Result); | |
} | |
private static int TimeintensiveMethod(object file) | |
{ | |
Console.WriteLine("Start TimeintensiveMethod."); | |
// Do some time intensive calculations... | |
using (StreamReader reader = new StreamReader(file.ToString())) | |
{ | |
string v = reader.ReadToEnd(); | |
for (int i = 0; i < 10000; i++) | |
v.GetHashCode(); | |
} | |
Console.WriteLine("End TimeintensiveMethod."); | |
return new Random().Next(100); | |
} | |
} | |
} |
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.Collections.Generic; | |
using System.Threading; | |
using System.Threading.Tasks; | |
namespace sebingel.TaskManagement | |
{ | |
public class Program | |
{ | |
static void Main() | |
{ | |
List<Task> taskList = new List<Task>(); | |
for (int i = 0; i < 10; i++) | |
{ | |
int j = i; | |
taskList.Add(Task.Factory.StartNew(() => Bla(j))); | |
} | |
Console.WriteLine("Everything added"); | |
Task.WaitAll(taskList.ToArray()); | |
Console.WriteLine("Everything endet"); | |
Console.ReadLine(); | |
} | |
private static void Bla(int i) | |
{ | |
Console.WriteLine("Thread {0} - Start {1}", Thread.CurrentThread.ManagedThreadId, i); | |
Thread.Sleep(1000); | |
Console.WriteLine("Thread {0} - End {1}", Thread.CurrentThread.ManagedThreadId, i); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment