Created
September 14, 2012 01:51
-
-
Save leandrosilva/3719342 to your computer and use it in GitHub Desktop.
Task parallelization with C# and Mono
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
#!/bin/bash | |
dmcs Program.cs ErrorLogger.cs ParallelSender.cs SenderTask.cs |
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; | |
namespace TaskParallelization | |
{ | |
class ErrorLogger | |
{ | |
private readonly IList<string> _messages; | |
public ErrorLogger() | |
{ | |
_messages = new List<string>(); | |
} | |
public void Log(string message) | |
{ | |
_messages.Add(message); | |
} | |
public bool Dump() | |
{ | |
try | |
{ | |
Console.WriteLine("Dumping error messages to log storage..."); | |
return true; | |
} | |
catch (Exception e) | |
{ | |
Console.WriteLine("An error dumping messages to log storage: {0}", e.Message); | |
return false; | |
} | |
} | |
} | |
} |
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.Tasks; | |
using System.Threading; | |
namespace TaskParallelization | |
{ | |
class ParallelSender | |
{ | |
private readonly IList<string> _contents; | |
private readonly ErrorLogger _errorLogger; | |
public ParallelSender(IList<string> contents) | |
{ | |
_contents = contents; | |
_errorLogger = new ErrorLogger(); | |
} | |
public void Send() | |
{ | |
Console.WriteLine("Sending {0} contents...", _contents.Count); | |
var sendingTasks = new List<Task>(); | |
foreach (var content in _contents) | |
{ | |
var senderTask = new SenderTask(content, _errorLogger); | |
sendingTasks.Add(Task.Factory.StartNew(() => senderTask.Do())); | |
} | |
var dumpingLogTask = Task.Factory.ContinueWhenAll( | |
sendingTasks.ToArray(), | |
(completedSendingTasks) => _errorLogger.Dump() | |
); | |
dumpingLogTask.Wait(); | |
Console.WriteLine("All contents were sent with sucess"); | |
} | |
} | |
} |
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; | |
using System.Collections.Generic; | |
namespace TaskParallelization | |
{ | |
class Program | |
{ | |
private static IList<string> Contents(int amount) | |
{ | |
var contents = new List<string>(); | |
for (int i = 1; i <= amount; i++) | |
{ | |
contents.Add("Content#" + i); | |
} | |
return contents; | |
} | |
static void Main(string[] args) | |
{ | |
Console.WriteLine(":: Task Parallelization ::"); | |
var amountOfContent = 0; | |
try { | |
amountOfContent = int.Parse(args[0]); | |
} catch (System.IndexOutOfRangeException) { | |
Console.WriteLine("\nUSAGE:"); | |
Console.WriteLine(" [1] $ mono Program.exe [number of tasks]"); | |
Console.WriteLine(" [2] $ ./test.sh [number of tasks]"); | |
Environment.Exit(-1); | |
} | |
Console.WriteLine("Amount of content to send = {0}", amountOfContent); | |
var sender = new ParallelSender(Contents(amountOfContent)); | |
sender.Send(); | |
Console.WriteLine("Finish"); | |
} | |
} | |
} |
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; | |
namespace TaskParallelization | |
{ | |
class SenderTask | |
{ | |
private readonly string _input; | |
private readonly ErrorLogger _errorLogger; | |
public SenderTask(string input, ErrorLogger errorLogger) | |
{ | |
_input = input; | |
_errorLogger = errorLogger; | |
} | |
public bool Do() | |
{ | |
try | |
{ | |
Console.WriteLine("- Sending {0}", _input); | |
return true; | |
} | |
catch (Exception e) | |
{ | |
_errorLogger.Log(String.Format("- An error sending {0}: {1}", _input, e.Message)); | |
return false; | |
} | |
} | |
} | |
} |
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
#!/bin/bash | |
mono Program.exe |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment