public IAsyncResult BeginGetContext(AsyncCallback callback, Object state) { CheckDisposed(); if (!listening) { throw new InvalidOperationException("Please, call Start before using this method."); } ListenerAsyncResult ares = new ListenerAsyncResult(callback, state); // lock wait_queue early to avoid race conditions lock (wait_queue) { lock (ctx_queue) { HttpListenerContext ctx = GetContextFromQueue(); if (ctx != null) { ares.Complete(ctx, true); return(ares); } } wait_queue.Add(ares); } return(ares); }
internal void RegisterContext(HttpListenerContext context) { try { Monitor.Enter(registry); registry [context] = context; Monitor.Enter(wait_queue); Monitor.Enter(ctx_queue); if (wait_queue.Count == 0) { ctx_queue.Add(context); } else { ListenerAsyncResult ares = (ListenerAsyncResult)wait_queue [0]; wait_queue.RemoveAt(0); ares.Complete(context); } } finally { Monitor.Exit(ctx_queue); Monitor.Exit(wait_queue); Monitor.Exit(registry); } }