Created
October 30, 2010 21:22
-
-
Save gamlerhart/655758 to your computer and use it in GitHub Desktop.
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
var client = new WebClient(); | |
var webContent = await client.DownloadStringTaskAsync("http://www.gamlor.info/wordpress/"); | |
ProcessFurther(webContent); |
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
We are on Thread No 10 | |
Downloaded 67770 characters, continue with the work | |
We are on Thread No 10 |
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
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); | |
} | |
} |
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
We are on Thread No 10 | |
Downloaded 67770 characters, continue with the work | |
We are on Thread No 16 |
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
We are on Thread No 10 | |
Downloaded 67770 characters, continue with the work | |
We are on Thread No 10 |
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
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); | |
} | |
} |
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
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fixed bug with your sync context. https://gist.github.com/2375443