Windows Service Controlle in VS2008
-
Hi, I hope some one out there can help me! I have been goggling my brains out all day tiring to find the answer for this question I have: I'm using visual studio 2008 and I am pretty much a noob to c#. So go easy on me. Trying to write a console app that checks the status of a windows service and if it is up stops it and if it's down brings it up! But the problem is that the book I am using to teach myself c# in vs 2008 does not describe this. And also to code that I do find on the web is for 2005 and doesn't work in 2008! I might look a bit liker this: public static void StartService(string serviceName, int timeoutMilliseconds) { ServiceController service = new ServiceController(serviceName); try { TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds); service.Start(); service.WaitForStatus(ServiceControllerStatus.Running, timeout); } catch { // ... } } But as I stated earlier the ServiceController class doesn't seem to work in 2008, or I could just be a dim whit... So any idea you might have as of how I might do this would make my day! Thanks in advice, Mike
-
Hi, I hope some one out there can help me! I have been goggling my brains out all day tiring to find the answer for this question I have: I'm using visual studio 2008 and I am pretty much a noob to c#. So go easy on me. Trying to write a console app that checks the status of a windows service and if it is up stops it and if it's down brings it up! But the problem is that the book I am using to teach myself c# in vs 2008 does not describe this. And also to code that I do find on the web is for 2005 and doesn't work in 2008! I might look a bit liker this: public static void StartService(string serviceName, int timeoutMilliseconds) { ServiceController service = new ServiceController(serviceName); try { TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds); service.Start(); service.WaitForStatus(ServiceControllerStatus.Running, timeout); } catch { // ... } } But as I stated earlier the ServiceController class doesn't seem to work in 2008, or I could just be a dim whit... So any idea you might have as of how I might do this would make my day! Thanks in advice, Mike
If you are not getting an exception how do you know the service has not started? Depending on what OS you are on, the OS properties are reported diffrently. Take a look at the win32 to .NET API cross refrence. Microsoft Win32 to Microsoft .NET Framework API Map[^]
-
Hi, I hope some one out there can help me! I have been goggling my brains out all day tiring to find the answer for this question I have: I'm using visual studio 2008 and I am pretty much a noob to c#. So go easy on me. Trying to write a console app that checks the status of a windows service and if it is up stops it and if it's down brings it up! But the problem is that the book I am using to teach myself c# in vs 2008 does not describe this. And also to code that I do find on the web is for 2005 and doesn't work in 2008! I might look a bit liker this: public static void StartService(string serviceName, int timeoutMilliseconds) { ServiceController service = new ServiceController(serviceName); try { TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds); service.Start(); service.WaitForStatus(ServiceControllerStatus.Running, timeout); } catch { // ... } } But as I stated earlier the ServiceController class doesn't seem to work in 2008, or I could just be a dim whit... So any idea you might have as of how I might do this would make my day! Thanks in advice, Mike
I could be way off here but if you're using Vista (or possibly Weven), IIRC you can only stop and start services if you add a manifest file and elevate to administrator (yeah - that brings in the UAC prompt for your app). It's a long time since I've tried this so I may be wrong, but I think that this is the case, and also if my memory serves correctly, you don't get any exception - it just doesn't work.
Dave
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
Why are you using VB6? Do you hate yourself? (Christian Graus) -
Hi, I hope some one out there can help me! I have been goggling my brains out all day tiring to find the answer for this question I have: I'm using visual studio 2008 and I am pretty much a noob to c#. So go easy on me. Trying to write a console app that checks the status of a windows service and if it is up stops it and if it's down brings it up! But the problem is that the book I am using to teach myself c# in vs 2008 does not describe this. And also to code that I do find on the web is for 2005 and doesn't work in 2008! I might look a bit liker this: public static void StartService(string serviceName, int timeoutMilliseconds) { ServiceController service = new ServiceController(serviceName); try { TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds); service.Start(); service.WaitForStatus(ServiceControllerStatus.Running, timeout); } catch { // ... } } But as I stated earlier the ServiceController class doesn't seem to work in 2008, or I could just be a dim whit... So any idea you might have as of how I might do this would make my day! Thanks in advice, Mike
Hi Mike, It did work for me on VS 2008. Here is my program - using System; using System.ServiceProcess; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { try { ServiceController service = new ServiceController("<<Your Service Name>>"); service.Stop(); TimeSpan timeout = TimeSpan.FromMilliseconds(10000); service.WaitForStatus(ServiceControllerStatus.Stopped, timeout); if (service.Status == ServiceControllerStatus.Stopped) { service.Start(); } } catch { // ... } } } } Are you getting any specific error ?
-
Hi Mike, It did work for me on VS 2008. Here is my program - using System; using System.ServiceProcess; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { try { ServiceController service = new ServiceController("<<Your Service Name>>"); service.Stop(); TimeSpan timeout = TimeSpan.FromMilliseconds(10000); service.WaitForStatus(ServiceControllerStatus.Stopped, timeout); if (service.Status == ServiceControllerStatus.Stopped) { service.Start(); } } catch { // ... } } } } Are you getting any specific error ?
First of all I would like to thank every one who has tried to help me thus far, it great! As I said im a noob at c# getting this type of help will bring me to the next level (even if its a still a low one ;)) I'm am using vista.... when using Shukla's great piece of code I get this: The problem might be missing a reference? Error 1 The type or namespace name 'ServiceProcess' does not exist in the namespace 'System' (are you missing an assembly reference?) C:\Users\mike\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs 2 14 ConsoleApplication1 The problem might be missing a reference? Also the ebook (Microsoft.Press.Microsoft.Visual.C.Sharp.2008.Step.by.Step)I use to teach myself is not telling me how to link in a external .dll so if you have any good references on this subject I would love to know! Anyway thank you for you time! :-D