Solved

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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 video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now