Click here to Skip to main content
15,922,584 members
Home / Discussions / C#
   

C#

 
GeneralRe: Error stop Http Listener Pin
N a v a n e e t h3-Jun-08 15:33
N a v a n e e t h3-Jun-08 15:33 
GeneralRe: Error stop Http Listener Pin
George_George3-Jun-08 15:49
George_George3-Jun-08 15:49 
GeneralRe: Error stop Http Listener Pin
N a v a n e e t h4-Jun-08 5:16
N a v a n e e t h4-Jun-08 5:16 
GeneralRe: Error stop Http Listener Pin
George_George4-Jun-08 20:34
George_George4-Jun-08 20:34 
GeneralRe: Error stop Http Listener Pin
N a v a n e e t h5-Jun-08 6:55
N a v a n e e t h5-Jun-08 6:55 
GeneralRe: Error stop Http Listener Pin
George_George5-Jun-08 14:44
George_George5-Jun-08 14:44 
GeneralRe: Error stop Http Listener Pin
N a v a n e e t h5-Jun-08 17:09
N a v a n e e t h5-Jun-08 17:09 
GeneralRe: Error stop Http Listener Pin
George_George5-Jun-08 18:50
George_George5-Jun-08 18:50 
Thanks N a v a n e e t h!


Is this your suggested solution?

Code change,

- removed IAsyncResult result;
- removed result.AsyncWaitHandle.WaitOne();
- change from result = _Server.BeginGetContext(new AsyncCallback(this.HttpCallback), _Server) to _Server.BeginGetContext(new AsyncCallback(this.HttpCallback), _Server);

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Net;
using System.Threading;

namespace TestServiceStop1
{
    public partial class Service1 : ServiceBase
    {
        private Thread _tHttpThread;
        private TestHttpServer _server;

        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            _server = new TestHttpServer(this);

            _tHttpThread = new Thread(_server.StartListen);

            _tHttpThread.Start();
        }

        protected override void OnStop()
        {
            // stop HTTP server
            _server.Stop(false);
        }
    }

    public class TestHttpServer
    {
        // listening HTTP port
        private int _Port = 0;

        // internal wrapped HTTP listener
        private HttpListener _Server = new HttpListener();

        private Service1 _manager;

        public TestHttpServer (Service1 manager)
        {
            _manager = manager;
        }

        public int ListenPort
        {
            get
            {
                return _Port;
            }
            set
            {
                _Port = value;
            }
        }

        public void StartListen()
        {
            try
            {

                _Server.Prefixes.Add(String.Format("http://+:{0}/", 9099));
                _Server.Start();
                while (true)
                {
									_Server.BeginGetContext(new AsyncCallback(this.HttpCallback), _Server);
                }
            }
            // any exceptions are not expected
            // catch InvalidOperationException during service stop
            // [System.InvalidOperationException] = {"Please call the Start() method before calling this method."}
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void Stop(bool isTerminate)
        {
            _Server.Stop();
        }

        // callback function when there is HTTP request received
        private void HttpCallback(IAsyncResult result)
        {
            HttpListenerContext context = _Server.EndGetContext(result);
            HandleRequest(context);
        }

        // find matched URL HTTP request handler and invoke related handler
        private void HandleRequest(HttpListenerContext context)
        {
            string matchUrl = context.Request.Url.AbsolutePath.Trim().ToLower();

            context.Response.StatusCode = 200;
            context.Response.StatusDescription = "OK";
            context.Response.Close();
        }
    } // end of Http Listener class
}



regards,
George
AnswerRe: Error stop Http Listener Pin
GDavy2-Jun-08 22:38
GDavy2-Jun-08 22:38 
GeneralRe: Error stop Http Listener Pin
George_George2-Jun-08 22:44
George_George2-Jun-08 22:44 
GeneralRe: Error stop Http Listener Pin
GDavy2-Jun-08 23:01
GDavy2-Jun-08 23:01 
GeneralRe: Error stop Http Listener Pin
George_George2-Jun-08 23:32
George_George2-Jun-08 23:32 
GeneralRe: Error stop Http Listener Pin
suzuuu26-Apr-10 20:52
suzuuu26-Apr-10 20:52 
AnswerRe: Error stop Http Listener Pin
Zoltan Balazs3-Jun-08 1:14
Zoltan Balazs3-Jun-08 1:14 
GeneralRe: Error stop Http Listener Pin
George_George3-Jun-08 1:23
George_George3-Jun-08 1:23 
QuestionHow i create auto update application Pin
wasimsharp2-Jun-08 21:46
wasimsharp2-Jun-08 21:46 
AnswerRe: How i create auto update application Pin
Robert Rohde2-Jun-08 21:53
Robert Rohde2-Jun-08 21:53 
QuestionHow to find if media file is DRM protected or not ? Pin
Sharma Ritesh2-Jun-08 21:44
Sharma Ritesh2-Jun-08 21:44 
AnswerRe: How to find if media file is DRM protected or not ? Pin
PeaceTiger8-Feb-11 9:15
PeaceTiger8-Feb-11 9:15 
QuestionQuestion on array?? Pin
Hum Dum2-Jun-08 19:46
Hum Dum2-Jun-08 19:46 
AnswerRe: Question on array?? Pin
dan!sh 2-Jun-08 20:03
professional dan!sh 2-Jun-08 20:03 
GeneralRe: Question on array?? Pin
Hum Dum2-Jun-08 20:16
Hum Dum2-Jun-08 20:16 
GeneralRe: Question on array?? Pin
KarlWF2-Jun-08 22:24
KarlWF2-Jun-08 22:24 
QuestionSending image from one project to another ? Pin
nomi2-Jun-08 19:12
nomi2-Jun-08 19:12 
AnswerRe: Sending image from one project to another ? Pin
Shree2-Jun-08 19:32
Shree2-Jun-08 19:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.