Solved

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

Posted on 2015-01-22
2
166 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How do I hide MVVM menu items in WPF/XAML? 1 31
jquery tab header text 1 27
asp web application 3 36
C# XML Get Values 4 33
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

685 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