| Author: Yuri Sho 22 Apr 2008 | Member Level: Bronze Points : 0 |
When I try happen this error:
System.InvalidOperationException = {"Cannot stop wuauserv service on computer '.'."}
InnerException = {"The service has not been started"}
System.ComponentModel.Win32Exception = {"The service has not been started"}
ErrorCode = -2147467259
NativeErrorCode = 1062
How to correct? ThX!
|
| Author: Vikas Kumar Singh 14 May 2008 | Member Level: Bronze Points : 2 |
Hi,
Check for the service's startup type if disable. Keep it either Automatic or Manual.
Regards, Vikas Singh
|
| Author: Sunil 09 Nov 2009 | Member Level: Bronze Points : 2 |
Here is the code with Exception handling. private void btnStart_Click(object sender, EventArgs e) { ServiceController sc = new System.ServiceProcess.ServiceController(strSVCName); try {
if (sc.Status.Equals(ServiceControllerStatus.Paused)) { sc.Continue(); sc.WaitForStatus(ServiceControllerStatus.Running); btnStart.Enabled = false; btnStop.Enabled = true; btnPause.Enabled = true; lblServiceStatus.Text = "Running"; } else { sc.Start(); sc.WaitForStatus(ServiceControllerStatus.Running); if (sc.Status.Equals(ServiceControllerStatus.Running)) { btnStart.Enabled = false; btnStop.Enabled = true; lblServiceStatus.Text = "Running"; if (sc.CanPauseAndContinue) btnPause.Enabled = true; } } } catch (Exception ex) { MessageBox.Show("Could not start " + strSVCName + " Service.\n Error : " + ex.Message.ToString()); } finally { sc.Close(); } }
private void btnStop_Click(object sender, EventArgs e) {
ServiceController sc = new System.ServiceProcess.ServiceController(strSVCName); try { sc.Stop(); sc.WaitForStatus(ServiceControllerStatus.Stopped);
if (sc.Status.Equals(ServiceControllerStatus.Stopped)) { btnStart.Enabled = true; btnStop.Enabled = false; btnPause.Enabled = false; lblServiceStatus.Text = "Stopped"; } } catch (Exception ex) { MessageBox.Show("Could not stop " + strSVCName + " Service.\n Error : " + ex.Message.ToString()); } finally { sc.Close(); } }
|
| Author: MathiasE 20 Nov 2009 | Member Level: Bronze Points : 2 |
Hi, Thanks for this great post! I tried it and it worked fine on a Windows XP system.
But on Windows 7 I get an error: "Service [ServiceName] cannot be opened on computer [MachineName]"
The service is a local service.
With "sc stop [ServiceName]" at the command-prompt I got: [SC] OpenService Error 5: Access denied Navigating through the options of services.msc, wmimgmt.msc, gpedit.msc I couldn't find the specific option to get this access. How is it possible to control that and to start and stop a service with the ServiceController-Class?
Thanks for help! MathiasE
|
| Author: Sunil 20 Nov 2009 | Member Level: Bronze Points : 1 |
Hi,
As always Vista onwards all the os are headache for developer. Open command run as an administrator. and then try. it will work.
Regards, Sunil G.
|
| Author: Sunil 21 Nov 2009 | Member Level: Bronze Points : 1 |
Hi,
As always Vista onwards all the os are headache for developer. Open command run as an administrator. and then try. it will work.
Regards, Sunil G.
|
| Author: MathiasE 21 Nov 2009 | Member Level: Bronze Points : 1 |
Thanks, Sunil! It works. But it seems no good idea, to tell users of my app to run it as administrator, since most of them aren't. (And the IT people do not want the service running permanently.) Is there no way to control a specific service access programmatically? Regards, MathiasE
|
| Author: Sunil 21 Nov 2009 | Member Level: Bronze Points : 1 |
Hi,
Just add administrator group to your application. This should work for u.
Regards, Sunil G.
|