WebSocketSharp.Net.HttpListener.BeginGetContext C# (CSharp) Method

BeginGetContext() public method

Begins getting an incoming request information asynchronously.
This asynchronous operation must be completed by calling the EndGetContext method. Typically, the method is invoked by the callback delegate.
/// This object has been closed. /// /// The has not been started or is stopped currently. ///
public BeginGetContext ( AsyncCallback callback, Object state ) : IAsyncResult
callback AsyncCallback /// An delegate that references the method(s) /// called when the asynchronous operation completes. ///
state Object /// An that contains a user defined object to pass to the delegate. ///
return IAsyncResult
        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 (((ICollection)wait_queue).SyncRoot) {
                lock (((ICollection)ctx_queue).SyncRoot) {
                    HttpListenerContext ctx = GetContextFromQueue ();
                    if (ctx != null) {
                        ares.Complete (ctx, true);
                        return ares;
                    }
                }

                wait_queue.Add (ares);
            }

            return ares;
        }

Usage Example

        // Loop here to begin processing of new requests.
        private void Listen()
        {
            while (IsListening)
            {
                if (_listener == null)
                {
                    return;
                }

                try
                {
                    _listener.BeginGetContext(ListenerCallback, _listener);
                    _listenForNextRequest.WaitOne();
                }
                catch (Exception ex)
                {
                    _logger.Error("Listen()", ex);
                    return;
                }
                if (_listener == null)
                {
                    return;
                }
            }
        }