Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Forcing GUI to wait on ServiceController.WaitForStatus

Posted on 2014-03-04
4
Medium Priority
?
891 Views
Last Modified: 2014-03-07
Hey Folks,

I have a windows app written in C# with the express purpose of restarting services prior to launch of another custom application, and I'm trying to get the GUI to pause until the service is fully stopped before firing the start method, but the GUI doesn't seem to halt on servicecontroller.waitforstatus();

Everything works, but once the service is stopped, the code immediately advances and skips the second if clause because the service hasn't fully been stopped as of yet, so the start() method never fires. I could build an artificial delay with a wait or a pause, but I'd prefer to do it properly and figure out the waitforstatus option. Any thoughts greatly appreciated.

My code looks like this:

ServiceController myService = new ServiceController(serviceName);
TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
string svcStatus = myService.Status.ToString();

 try
            {

               
                if (svcStatus == "Running")
                {
                    myService.Stop();
                    myService.Refresh();
                    myService.WaitForStatus(ServiceControllerStatus.Stopped);
                }
               
                //once service is stopped, start it.
                if (svcStatus == "Stopped")
                {
                    myService.Start();
                    myService.Refresh();
                    myService.WaitForStatus(ServiceControllerStatus.Running);

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "\n\n" + ex.Source + "\n\n" + ex.InnerException);

            }
0
Comment
Question by:FroggedUp
[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
4 Comments
 
LVL 18

Accepted Solution

by:
Dennis Aries earned 1000 total points
ID: 39906395
Except for the fact that you check the same status twice, there is nothing wrong with your code. However, if your service returns control to you before it is stopped, the you are in trouble.

I've tried your code with a random service on my machine and it worked like a charm.

Dennis
0
 
LVL 16

Assisted Solution

by:SriVaddadi
SriVaddadi earned 1000 total points
ID: 39906896
Try this code

ServiceController myService = new ServiceController(serviceName);
TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
//string svcStatus = myService.Status.ToString();

 try
            {

               
                if (myService.Status == ServiceControllerStatus.Running)
                {
                    myService.Stop();
                    myService.Refresh();
                    myService.WaitForStatus(ServiceControllerStatus.Stopped);
                }
               
                //once service is stopped, start it.
                if (myService.Status == ServiceControllerStatus.Stopped)
                {
                    myService.Start();
                    myService.Refresh();
                    myService.WaitForStatus(ServiceControllerStatus.Running);

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "\n\n" + ex.Source + "\n\n" + ex.InnerException);

            }

I think svcStatus will always has "running" when it is hitting the second if clause as you are not re-querying the status.
0
 

Author Comment

by:FroggedUp
ID: 39910996
Duh, that was really stupid of me. without setting the status to the variable after each run, of course the start() call wouldn't fire. Whoops. Thanks guy!
0
 
LVL 16

Expert Comment

by:SriVaddadi
ID: 39911465
This is not right. I pointed out the issue and my comment should be solution than assisted solution.
0

Featured Post

Quick Start: DOCKER

Sometimes you just need a Quick Start on a topic in order to begin using it.. this is just what you need to know to get up and running with Docker!

Question has a verified solution.

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

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

715 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