Created
July 5, 2011 03:29
-
-
Save txdv/1064214 to your computer and use it in GitHub Desktop.
Proposed boundary extensions
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 Manos; | |
using Manos.IO; | |
using Manos.Threading; | |
using Manos.IO.Managed; | |
using System.Threading; | |
namespace Manos | |
{ | |
public static class BoundaryExtensions | |
{ | |
public static void Join(this Boundary boundary, Action action, Action success) | |
{ | |
if (action != null) { | |
ThreadPool.QueueUserWorkItem(delegate { | |
action(); | |
boundary.ExecuteOnTargetLoop(delegate { | |
if (success != null) { | |
success(); | |
} | |
}); | |
}); | |
} | |
} | |
public static void Join<T>(this Boundary boundary, Func<T> action, Action<T> success) | |
{ | |
if (action != null) { | |
ThreadPool.QueueUserWorkItem(delegate { | |
T res = action(); | |
boundary.ExecuteOnTargetLoop(delegate { | |
if (success != null) { | |
success(res); | |
} | |
}); | |
}); | |
} | |
} | |
} | |
public static class ProposedBoundary | |
{ | |
private static Dictionary<Context, Boundary> boundaries = new Dictionary<Context, Boundary>(); | |
private static Boundary GetBoundary(Context context) | |
{ | |
if (!boundaries.ContainsKey(context)) { | |
boundaries[context] = new Boundary(context); | |
} | |
return boundaries[context]; | |
} | |
public static void Join(Context context, Action action, Action success) | |
{ | |
GetBoundary(context).Join(action, success); | |
} | |
public static void Join<T>(Context context, Func<T> action, Action<T> success) | |
{ | |
GetBoundary(context).Join<T>(action, success); | |
} | |
} | |
class MainClass | |
{ | |
public void Main(string[] args) | |
{ | |
Context context = Context.Create(); | |
Boundary boundary = new Boundary(context); | |
boundary.Join(delegate { | |
Console.WriteLine ("1"); | |
Thread.Sleep(2000); | |
return Tuple.Create(2, "nesamone"); | |
}, delegate (Tuple<int, string> result) { | |
Console.WriteLine(result.Item1); | |
}); | |
boundary.Join(delegate { | |
Console.WriteLine("one"); | |
Thread.Sleep(1000); | |
}, delegate { | |
Console.WriteLine("two"); | |
}); | |
context.Start(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment