Solved

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

Posted on 2015-01-22
2
168 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Raise the IQ of Your IT Alerts

From IT major incidents to manufacturing line slowdowns, every business process generates insights that need to reach the people required to take action. You need a platform that integrates with your business tools to create fully enabled DevOps toolchains.

You need xMatters.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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.
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

690 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