I play with HttpListener and Async CTP
class HttpServer : IDisposable
{
HttpListener listener;
CancellationTokenSource cts;
public void Start()
{
listener = new HttpListener();
listener.Prefixes.Add("http://+:1288/test/");
listener.Start();
cts = new CancellationTokenSource();
Listen();
}
public void Stop()
{
cts.Cancel();
}
int counter = 0;
private async void Listen()
{
while (!cts.IsCancellationRequested)
{
HttpListenerContext context = await listener.GetContextAsyncTask();
Console.WriteLine("Client connected " + ++counter);
await TaskEx.Delay(5000, cts.Token);
Console.WriteLine("Response " + counter);
}
listener.Close();
}
}
I was expecting the following output when 3 clients connect at the same time
Client connected 1
Client connected 2
Client connected 3
<5000ms pause>
Response 1
Response 2
Response 3
Instead i get
Client connected 1
<5000ms pause>
Response 1
Client connected 2
<5000ms pause>
Response 2
Client connected 3
<5000ms pause>
Response 3
If I use the sequel, it works as I expected
int c = counter;
TaskEx.Delay(5000, cts.Token).ContinueWith(t => {
Console.WriteLine("Response " + c);
});
I had the impression that it await TaskEx.Delaywould immediately return (and go to while (!cts.IsCancellationRequested)), and the remainder of the while block would continue after 5000 ms. So it should be the same as my code with .ContinueWith, no?
source
share