Solved

Forcing GUI to wait on ServiceController.WaitForStatus

Posted on 2014-03-04
4
826 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 250 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 250 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
itextsharp with c# 3 18
IDE for Python 5 73
I need help adding validation to my MVC.Net view 6 23
Include MS Database in Winforms Project using Intellilock 5 21
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…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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 an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

730 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