In addition, if you want line-by-line processing for a limited time, BeginGetContext returns System.IAsyncResult displaying the AsyncWaitHandle property
var context = listener.BeginGetContext(new AsyncCallback(ListenerCallback), listener); context.AsyncWaitHandle.WaitOne();
Above, it blocks the stream until the listener receives something real, as defined by the headers assigned to the listener, or ends due to some exception that terminates the listener stream and returns the result back to the ListenerCallback.
But AsyncWaitHandle.WaitOne () can accept timeout parameters
// 5 seconds timeout bool success = context.AsyncWaitHandle.WaitOne(5000, true); if (success == false) { throw new Exception("Timeout waiting for http request."); }
ListenerCallback may contain a call to listener.EndGetContext or just call listener.EndGetContext in a string if there is no timeout or error specified in AsyncWaitHandle
public static void ListenerCallback(IAsyncResult result) { HttpListener listener = (HttpListener) result.AsyncState;
Remember to listen again using listener.BeginGetContext
tofo
source share