Skip to main content
Question Protected by qwertymk
deleted 24 characters in body
Source Link
prostynick
  • 6.2k
  • 5
  • 42
  • 61

I have a listener:

listener = new HttpListener(); listener.Prefixes.Add(@"http://+:8077/"); listener.Start(); listenerThread = new Thread(HandleRequests); listenerThread.Start(); 

And I am handling requests:

private void HandleRequests() { while (listener.IsListening) { var context = listener.BeginGetContext(new AsyncCallback(ListenerCallback), listener); context.AsyncWaitHandle.WaitOne(); } } private void ListenerCallback(IAsyncResult ar) { var listener = ar.AsyncState as HttpListener; var context = listener.EndGetContext(ar); //do some stuff } 

I would like to write void Stop() in such a way, that:

  1. It will block until all currently handled requests will end (ie. will wait for all threads to "do some stuff").
  2. While it will wait for already started requests, it will not allow any more requests (ie. return at the beginning of ListenerCallback).
  3. After that it will call listener.Stop() (listener.IsListening became false).

How could it be write?

EDIT: What do you think about this solution? Is it safe?

public void Stop() { lock (this) { isStopping = true; } resetEvent.WaitOne(); //initially set to true listener.Stop(); } private void ListenerCallback(IAsyncResult ar) { lock (this) { if (isStopping) return; resetEvent.Reset(); }  Interlocked.Increment(refnumberOfRequests++;  numberOfRequests); } var listener = ar.AsyncState as HttpListener; var context = listener.EndGetContext(ar); //do some stuff iflock (Interlocked.Decrement(refthis)  { if (--numberOfRequests) == 0)   resetEvent.Set(); } } 

I have a listener:

listener = new HttpListener(); listener.Prefixes.Add(@"http://+:8077/"); listener.Start(); listenerThread = new Thread(HandleRequests); listenerThread.Start(); 

And I am handling requests:

private void HandleRequests() { while (listener.IsListening) { var context = listener.BeginGetContext(new AsyncCallback(ListenerCallback), listener); context.AsyncWaitHandle.WaitOne(); } } private void ListenerCallback(IAsyncResult ar) { var listener = ar.AsyncState as HttpListener; var context = listener.EndGetContext(ar); //do some stuff } 

I would like to write void Stop() in such a way, that:

  1. It will block until all currently handled requests will end (ie. will wait for all threads to "do some stuff").
  2. While it will wait for already started requests, it will not allow any more requests (ie. return at the beginning of ListenerCallback).
  3. After that it will call listener.Stop() (listener.IsListening became false).

How could it be write?

EDIT: What do you think about this solution? Is it safe?

public void Stop() { lock (this) { isStopping = true; } resetEvent.WaitOne(); //initially set to true listener.Stop(); } private void ListenerCallback(IAsyncResult ar) { lock (this) { if (isStopping) return; resetEvent.Reset(); }  Interlocked.Increment(ref numberOfRequests); var listener = ar.AsyncState as HttpListener; var context = listener.EndGetContext(ar); //do some stuff if (Interlocked.Decrement(ref numberOfRequests) == 0) resetEvent.Set(); } 

I have a listener:

listener = new HttpListener(); listener.Prefixes.Add(@"http://+:8077/"); listener.Start(); listenerThread = new Thread(HandleRequests); listenerThread.Start(); 

And I am handling requests:

private void HandleRequests() { while (listener.IsListening) { var context = listener.BeginGetContext(new AsyncCallback(ListenerCallback), listener); context.AsyncWaitHandle.WaitOne(); } } private void ListenerCallback(IAsyncResult ar) { var listener = ar.AsyncState as HttpListener; var context = listener.EndGetContext(ar); //do some stuff } 

I would like to write void Stop() in such a way, that:

  1. It will block until all currently handled requests will end (ie. will wait for all threads to "do some stuff").
  2. While it will wait for already started requests, it will not allow any more requests (ie. return at the beginning of ListenerCallback).
  3. After that it will call listener.Stop() (listener.IsListening became false).

How could it be write?

EDIT: What do you think about this solution? Is it safe?

public void Stop() { lock (this) { isStopping = true; } resetEvent.WaitOne(); //initially set to true listener.Stop(); } private void ListenerCallback(IAsyncResult ar) { lock (this) { if (isStopping) return; resetEvent.Reset(); numberOfRequests++;   } var listener = ar.AsyncState as HttpListener; var context = listener.EndGetContext(ar); //do some stuff lock (this)  { if (--numberOfRequests == 0)   resetEvent.Set(); } } 
added 627 characters in body; deleted 20 characters in body
Source Link
prostynick
  • 6.2k
  • 5
  • 42
  • 61

I have a listener:

listener = new HttpListener(); listener.Prefixes.Add(@"http://+:8077/"); listener.Start(); listenerThread = new Thread(HandleRequests); listenerThread.Start(); 

And I am handling requests:

private void HandleRequests() { while (listener.IsListening) { var context = listener.BeginGetContext(new AsyncCallback(ListenerCallback), listener); context.AsyncWaitHandle.WaitOne(); } } private void ListenerCallback(IAsyncResult ar) { var listener = ar.AsyncState as HttpListener; var context = listener.EndGetContext(ar); //do some stuff } 

I would like to write void Stop() in such a way, that:

  1. It will block until all currently handled requests will end (ie. will wait for all threads to "do some stuff").
  2. While it will wait for already started requests, it will not allow any more requests (ie. return at the beginning of ListenerCallback).
  3. After that it will call listener.Stop() (listener.IsListening became false).

How could it be write?

EDIT: What do you think about this solution? Is it safe?

public void Stop() { lock (this) { isStopping = true; } resetEvent.WaitOne(); //initially set to true listener.Stop(); } private void ListenerCallback(IAsyncResult ar) { lock (this) { if (isStopping) return; resetEvent.Reset(); } Interlocked.Increment(ref numberOfRequests); var listener = ar.AsyncState as HttpListener; var context = listener.EndGetContext(ar); //do some stuff if (Interlocked.Decrement(ref numberOfRequests) == 0) resetEvent.Set(); } 

I have a listener:

listener = new HttpListener(); listener.Prefixes.Add(@"http://+:8077/"); listener.Start(); listenerThread = new Thread(HandleRequests); listenerThread.Start(); 

And I am handling requests:

private void HandleRequests() { while (listener.IsListening) { var context = listener.BeginGetContext(new AsyncCallback(ListenerCallback), listener); context.AsyncWaitHandle.WaitOne(); } } private void ListenerCallback(IAsyncResult ar) { var listener = ar.AsyncState as HttpListener; var context = listener.EndGetContext(ar); //do some stuff } 

I would like to write void Stop() in such a way, that:

  1. It will block until all currently handled requests will end (ie. will wait for all threads to "do some stuff").
  2. While it will wait for already started requests, it will not allow any more requests (ie. return at the beginning of ListenerCallback).
  3. After that it will call listener.Stop() (listener.IsListening became false).

How could it be write?

I have a listener:

listener = new HttpListener(); listener.Prefixes.Add(@"http://+:8077/"); listener.Start(); listenerThread = new Thread(HandleRequests); listenerThread.Start(); 

And I am handling requests:

private void HandleRequests() { while (listener.IsListening) { var context = listener.BeginGetContext(new AsyncCallback(ListenerCallback), listener); context.AsyncWaitHandle.WaitOne(); } } private void ListenerCallback(IAsyncResult ar) { var listener = ar.AsyncState as HttpListener; var context = listener.EndGetContext(ar); //do some stuff } 

I would like to write void Stop() in such a way, that:

  1. It will block until all currently handled requests will end (ie. will wait for all threads to "do some stuff").
  2. While it will wait for already started requests, it will not allow any more requests (ie. return at the beginning of ListenerCallback).
  3. After that it will call listener.Stop() (listener.IsListening became false).

How could it be write?

EDIT: What do you think about this solution? Is it safe?

public void Stop() { lock (this) { isStopping = true; } resetEvent.WaitOne(); //initially set to true listener.Stop(); } private void ListenerCallback(IAsyncResult ar) { lock (this) { if (isStopping) return; resetEvent.Reset(); } Interlocked.Increment(ref numberOfRequests); var listener = ar.AsyncState as HttpListener; var context = listener.EndGetContext(ar); //do some stuff if (Interlocked.Decrement(ref numberOfRequests) == 0) resetEvent.Set(); } 
Source Link
prostynick
  • 6.2k
  • 5
  • 42
  • 61

Multi-threading with .Net HttpListener

I have a listener:

listener = new HttpListener(); listener.Prefixes.Add(@"http://+:8077/"); listener.Start(); listenerThread = new Thread(HandleRequests); listenerThread.Start(); 

And I am handling requests:

private void HandleRequests() { while (listener.IsListening) { var context = listener.BeginGetContext(new AsyncCallback(ListenerCallback), listener); context.AsyncWaitHandle.WaitOne(); } } private void ListenerCallback(IAsyncResult ar) { var listener = ar.AsyncState as HttpListener; var context = listener.EndGetContext(ar); //do some stuff } 

I would like to write void Stop() in such a way, that:

  1. It will block until all currently handled requests will end (ie. will wait for all threads to "do some stuff").
  2. While it will wait for already started requests, it will not allow any more requests (ie. return at the beginning of ListenerCallback).
  3. After that it will call listener.Stop() (listener.IsListening became false).

How could it be write?