Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Forcing GUI to wait on ServiceController.WaitForStatus

Posted on 2014-03-04
4
Medium Priority
?
935 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
  • 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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

783 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