private void ListenerCallback(IAsyncResult ar)
        {
            HttpListenerCallbackState callbackState = (HttpListenerCallbackState)ar.AsyncState;
            HttpListenerContext       context       = null;

            //
            try
            {
                context = callbackState.Listener.EndGetContext(ar);
            }
            catch (Exception ex)
            {
                Console.WriteLine("WebServiceGateway: " + ex.Message + "\n" + ex.StackTrace);
            }
            finally
            {
                callbackState.ListenForNextRequest.Set();
            }
            if (context == null)
            {
                return;
            }
            //
            Worker(context);
        }
 private void ListenAsynchronously(IEnumerable<string> prefixes)
 {
     HttpListener listener = new HttpListener();
     foreach (string s in prefixes)
     {
         listener.Prefixes.Add(s);
     }
     listener.Start();
     HttpListenerCallbackState state = new HttpListenerCallbackState(listener);
     ThreadPool.QueueUserWorkItem(Listen, state);
 }
        private void StartListener(IEnumerable <string> prefixes)
        {
            stopEvent.Reset();
            HttpListener listener = new HttpListener();

            foreach (string s in prefixes)
            {
                listener.Prefixes.Add(s);
            }
            listener.Start();
            HttpListenerCallbackState state = new HttpListenerCallbackState(listener);

            ThreadPool.QueueUserWorkItem(Listen, state);
        }
 private void Listen(object state)
 {
     HttpListenerCallbackState callbackState = (HttpListenerCallbackState)state;
     while (callbackState.Listener.IsListening)
     {
         callbackState.Listener.BeginGetContext(new AsyncCallback(ListenerCallback), callbackState);
         int n = WaitHandle.WaitAny(new WaitHandle[] { callbackState.ListenForNextRequest, stopEvent });
         if (n == 1)
         {
             // stopEvent was signalled 
             callbackState.Listener.Stop();
             break;
         }
     }
 }
        private void StartListener(IEnumerable <string> prefixes)
        {
            stopEvent.Reset();
            HttpListener listener = new HttpListener();

            if (servicePassword != "")
            {
                listener.AuthenticationSchemes = AuthenticationSchemes.Basic;
            }
            foreach (string s in prefixes)
            {
                listener.Prefixes.Add(s);
            }
            listener.Start();
            HttpListenerCallbackState state = new HttpListenerCallbackState(listener);

            ThreadPool.QueueUserWorkItem(Listen, state);
        }
        private void Listen(object state)
        {
            HttpListenerCallbackState callbackState = (HttpListenerCallbackState)state;

            while (callbackState.Listener.IsListening)
            {
                callbackState.Listener.BeginGetContext(new AsyncCallback(ListenerCallback), callbackState);
                int n = WaitHandle.WaitAny(new WaitHandle[] { callbackState.ListenForNextRequest, stopEvent });
                if (n == 1)
                {
                    // stopEvent was signaled
                    try
                    {
                        callbackState.Listener.Stop();
                    }
                    catch (Exception e)
                    {
                        MigService.Log.Error(e);
                    }
                    break;
                }
            }
        }
        private void ListenerCallback(IAsyncResult result)
        {
            HttpListenerCallbackState callbackState = (HttpListenerCallbackState)result.AsyncState;
            HttpListenerContext       context       = null;

            callbackState.ListenForNextRequest.Set();
            try
            {
                context = callbackState.Listener.EndGetContext(result);
            }
            catch (Exception ex)
            {
                MigService.Log.Error(ex);
            }
            //finally
            //{
            //    callbackState.ListenForNextRequest.Set();
            //}
            if (context == null)
            {
                return;
            }
            Worker(context);
        }
 private void ListenAsynchronously(IEnumerable<string> prefixes)
 {
     HttpListener listener = new HttpListener();
     foreach (string s in prefixes)
     {
         listener.Prefixes.Add(s);
     }
     listener.Start();
     HttpListenerCallbackState state = new HttpListenerCallbackState(listener);
     ThreadPool.QueueUserWorkItem(Listen, state);
 }
 private void StartListener(IEnumerable<string> prefixes)
 {
     stopEvent.Reset();
     HttpListener listener = new HttpListener();
     foreach (string s in prefixes)
     {
         listener.Prefixes.Add(s);
     }
     listener.Start();
     HttpListenerCallbackState state = new HttpListenerCallbackState(listener);
     ThreadPool.QueueUserWorkItem(Listen, state);
 }
 private void StartListener(IEnumerable<string> prefixes)
 {
     stopEvent.Reset();
     HttpListener listener = new HttpListener();
     if (servicePassword != "")
     {
         listener.AuthenticationSchemes = AuthenticationSchemes.Basic;
     }
     foreach (string s in prefixes)
     {
         listener.Prefixes.Add(s);
     }
     listener.Start();
     HttpListenerCallbackState state = new HttpListenerCallbackState(listener);
     ThreadPool.QueueUserWorkItem(Listen, state);
 }