Dendei
Dendei

Reputation: 571

My windows service cant stop, after i implemented Thread

My service is installed by wix and starts all fine and did work like i wanted until i added a Thread to my service, so probably i dont shut down the Thread correctly

Here is the Service

public class WCFWindowsService : ServiceBase
{
    public ServiceHost serviceHost = null;

    public WCFWindowsService()
    {
        // Name the Windows Service
        ServiceName = "ServiceName";
    }

    public static void Main()
    {
        ServiceBase.Run(new WCFWindowsService()); 
    }

    protected override void OnStart(string[] args)
    {
        ThreadTheClass T = new ThreadTheClass();

            if (serviceHost != null)
            {
                serviceHost.Close();
            }
            Thread _thread = new Thread(T.ThreadMain);
            _thread = new Thread(T.ThreadMain);
            _thread.Start();

            serviceHost = new ServiceHost(typeof(ProjectWCF.WCFService));
            serviceHost.Open(); 
    }

    protected override void OnStop()
    {
        ThreadTheClass T = new ThreadTheClass();

            if (serviceHost != null)
            {
                WCFWindowsService ThreadPost = new WCFWindowsService();

                T.ThreadStop();


                serviceHost.Close();
                serviceHost = null;
            }
    }
}

And the Thread Class

class ThreadTheClass
{
    System.Threading.Timer MyTimer;
    public void ThreadMain()
    {
        int Minutes = 2;
        MyTimer = new System.Threading.Timer((s) =>
        {
            Logic();
        }
        , null, 5000, Minutes * 100 * 60);

    }

    public void ThreadStop()
    {
        MyTimer.Dispose();
    }

    void Logic()
    {
        //do things every two minutes 
    }

I dont know what is wrong cause when i try this in console program, The ThreadStop() works fine and shuts down the thread so why cant it shut down in windows service?

i get this error when i try to stop the installed service enter image description here

Upvotes: 0

Views: 888

Answers (1)

Damien_The_Unbeliever
Damien_The_Unbeliever

Reputation: 239814

The most obvious error is here, in OnStop:

ThreadTheClass T = new ThreadTheClass();

That's creating a new instance of ThreadTheClass. One for which MyTimer will be null.

Instead, in your OnStart method, you need to store the instance that you're creating there, so that OnStop can access that same instance.

ThreadTheClass T;

 protected override void OnStart(string[] args)
    {
        T = new ThreadTheClass();

            if (serviceHost != null)
            {
                serviceHost.Close();
            }
            T.ThreadMain();

            serviceHost = new ServiceHost(typeof(ProjectWCF.WCFService));
            serviceHost.Open(); 
    }

    protected override void OnStop()
    {
            if (serviceHost != null)
            {
                WCFWindowsService ThreadPost = new WCFWindowsService();

                T.ThreadStop();


                serviceHost.Close();
                serviceHost = null;
            }
    }

I've also removed the code that created (2!) new Threads - since ThreadMain just establishes a timer and then exits, there's no reason to put that on its own thread.

Upvotes: 1

Related Questions