Skip to content

Instantly share code, notes, and snippets.

@gamlerhart
Created October 30, 2010 21:22
Show Gist options
  • Save gamlerhart/655758 to your computer and use it in GitHub Desktop.
Save gamlerhart/655758 to your computer and use it in GitHub Desktop.
var client = new WebClient();
var webContent = await client.DownloadStringTaskAsync("http://www.gamlor.info/wordpress/");
ProcessFurther(webContent);
We are on Thread No 10
Downloaded 67770 characters, continue with the work
We are on Thread No 10
class MyPrimitiveSynchronisationContext : SynchronizationContext
{
private readonly Queue<Action> messagesToProcess = new Queue<Action>();
private readonly object syncHandle = new object();
private bool isRunning = true;
public override void Send(SendOrPostCallback codeToRun, object state)
{
throw new NotImplementedException();
}
public override void Post(SendOrPostCallback codeToRun, object state)
{
lock (syncHandle)
{
messagesToProcess.Enqueue(() => codeToRun(state));
SignalContinue();
}
}
public void RunMessagePump()
{
while(CanContinue())
{
Action nextToRun = GrabItem();
nextToRun();
}
}
private Action GrabItem()
{
lock (syncHandle)
{
while (CanContinue() && messagesToProcess.Count == 0)
{
Monitor.Wait(syncHandle);
}
return messagesToProcess.Dequeue();
}
}
private bool CanContinue()
{
lock (syncHandle)
{
return isRunning;
}
}
public void Cancel()
{
lock (syncHandle)
{
isRunning = false;
SignalContinue();
}
}
private void SignalContinue()
{
Monitor.Pulse(syncHandle);
}
}
We are on Thread No 10
Downloaded 67770 characters, continue with the work
We are on Thread No 16
We are on Thread No 10
Downloaded 67770 characters, continue with the work
We are on Thread No 10
class Program
{
static void Main(string[] args)
{
// Setup our synchronisation context
MyPrimitiveSynchronisationContext ctx = new MyPrimitiveSynchronisationContext();
MyPrimitiveSynchronisationContext.SetSynchronizationContext(ctx);
// The first thing to process is our main application
ctx.Post(obj=>
MainProgramm(), null);
// Then we kick of the message pump
ctx.RunMessagePump();
}
static async void MainProgramm(){
Console.Out.WriteLine("We are on Thread No {0}", Thread.CurrentThread.ManagedThreadId);
var client = new WebClient();
var webContentHomePage = await client.DownloadStringTaskAsync("http://www.gamlor.info/wordpress/");
Console.Out.WriteLine("Downloaded {0} characters, continue with the work", webContentHomePage.Length);
Console.Out.WriteLine("We are on Thread No {0} ", Thread.CurrentThread.ManagedThreadId);
}
}
Console.Out.WriteLine("We are on Thread No {0}",Thread.CurrentThread.ManagedThreadId);
var client = new WebClient();
var webContentHomePage = await client.DownloadStringTaskAsync("http://www.gamlor.info/wordpress/");
Console.Out.WriteLine("We are on Thread No {0}",Thread.CurrentThread.ManagedThreadId);
Console.Out.WriteLine("Page is {0} characters long",webContentHomePage.Length);
@lessneek
Copy link

Fixed bug with your sync context. https://gist.github.com/2375443

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment