Solved

How to stop windows service when the threads in the service are stopped running?

Posted on 2015-01-22
2
165 Views
Last Modified: 2015-01-22
I have a windows service running with multiple threads. At some point of time if all the threads are stopped because of any reason how do I stop my service? And where should I write the code? Right now even if all the threads are stopped, service is still running.
protected override void OnStart(string[] args)
        {
            _thread = new Thread(ThreadCaller);
            _thread.Priority = ThreadPriority.Highest;
            _thread.Name = "Message Publlishing, Receiving and Processing Service";
            _thread.IsBackground = true;
            _thread.Start();
        }

        /// <summary>
        /// Stops both Objects and releases the resources.
        /// </summary>
        protected override void OnStop()
        {
            Utilities.StopEntireProcess = true;
            Logger.log.Info("Threads are getting stopped...");

            gaosMsgRecvingService._msgRecvingThread.Join();
            gaosMsgPublishingService._msgPublishingThread.Join();
            gaosMsgProcessingService._msgProcessingThread.Join();

            gaosMsgRecvingService = null;
            gaosMsgPublishingService = null;
            gaosMsgProcessingService = null;

            //eventLog1.WriteEntry("All Threads are stopped.");
            Logger.log.Info("All Threads are stopped.");

            _thread.Join();
            _thread = null;
        }

        public void ThreadCaller()
        {
            try
            {
                gaosMsgRecvingService = new GaosMsgRecvingService();
                gaosMsgPublishingService = new GAOSMsgPublishingService();
                gaosMsgProcessingService = new GaosMsgProcessingService();

                gaosMsgRecvingService.Start();
                gaosMsgPublishingService.Start();
                gaosMsgProcessingService.Start();
            }
            catch (Exception ex)
            {
                Logger.log.Error(ex);
            }
        }

Open in new window

0
Comment
Question by:GouthamAnand
  • 2
2 Comments
 

Accepted Solution

by:
GouthamAnand earned 0 total points
ID: 40565327
I have implemented timer to check if all the threads are stopped. It is success.

 private void ServiceTimer()
        {
            aTimer = new System.Timers.Timer();
            aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
            aTimer.Interval = 30000;
            aTimer.Enabled = true;  
        }

        private void OnTimedEvent(object source, ElapsedEventArgs e)
        {
            while (!gaosMsgRecvingService.IsAlive() && !gaosMsgProcessingService.IsAlive() && !gaosMsgProcessingService.IsAlive())
            {
                controller = new ServiceController(Process.GetCurrentProcess().ProcessName.ToString());
                controller.Stop();
                controller.Close();
            }
        }
0
 

Author Closing Comment

by:GouthamAnand
ID: 40565329
Thanks & Regards,
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question