Solved

Forcing GUI to wait on ServiceController.WaitForStatus

Posted on 2014-03-04
4
763 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 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
This is not right. I pointed out the issue and my comment should be solution than assisted solution.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Using Simpleject with Class Library 11 25
asp.net bundle 8 34
Exception in Log4Net 1 18
Check my code out(2) 1 26
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

743 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

17 Experts available now in Live!

Get 1:1 Help Now