Solved

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

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
array not updating 8 42
C# LINQ 5 42
Load XML element 3 43
.NET Redemption/Outlook Examples 3 32
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

734 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