views:

45

answers:

0

Hi,

I created an application which hosts a RESTful service in its own thread. When I close the application I noticed that some resources are still running.

I would be very thankful if anyone could take a look at the code for initializing, starting and stopping the thread and suggest some better way for doing it.

These are the methods for starting and stopping the RESTful service:

public void StartRestService()
    {
        WebHttpBinding binding = new WebHttpBinding();
        RESTServiceHost = new ServiceHost(typeof(GaitLinkService), new Uri("http://localhost:8000/GaitLink"));
        RESTServiceHost.AddServiceEndpoint(typeof(IGaitLinkService), binding, "GaitLink");
        RESTServiceHost.Open();
    }

    public void StopRestService()
    {
        if (RESTServiceHost.State != CommunicationState.Closed)
        {
            RESTServiceHost.Close();
        }
    }

And these are the methods I created for starting and stopping the thread:

private void initializeRestServerThread()
    {
        restServer = new RestServiceClass();
        restServerThread = new Thread(new ThreadStart(restServer.StartRestService));
    }

    private void startRestServerThread()
    {
        initializeRestServerThread();
        restServerThread.Start();
    }

    private void stopRestServerThread()
    {
        restServer.StopRestService();
        try
        {
            restServerThread.Join();
        }
        catch(Exception ex)
        {
            string sDummy = ex.ToString();
        }

    }

When application starts I call startRestServerThread();

And OnForClosing I call the method stopRestServerThread:

private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
    {
        stopRestServerThread();

    }

Thanks!