Hi,
I have debug my code is working correctly only the issue was service name was wrong.
Regards,
Mahzar Khan
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I am trying to building generic tool from which we can control any windows service in the window operating system using asp.net window app, i got this references: https://stackoverflow.com/questions/19094712/control-the-windows-service i have added two buttons start and stop from below code, but i am getting the error in exception: {"Cannot start service aspnet_state on computer '.'."}
.
public Form3()
{
InitializeComponent();
}
private void start_Click(object sender, EventArgs e)
{
string serviceName = "aspnet_state";
double timeoutMilliseconds = 10_000;
TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
ServiceController service = new ServiceController(serviceName);
try
{
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running, timeout);
}
catch (InvalidOperationException exc)
{
MessageBox.Show($"The service failed to start in a timely manner\n\n{exc}");
}
}
private void stop_Click(object sender, EventArgs e)
{
string serviceName = "aspnet_state";
double timeoutMilliseconds = 10_000;
TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
ServiceController service = new ServiceController(serviceName);
try
{
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
}
catch (InvalidOperationException exc)
{
MessageBox.Show($"The service failed to start in a timely manner\n\n{exc}");
}
}
Hi,
I have debug my code is working correctly only the issue was service name was wrong.
Regards,
Mahzar Khan
Hi mohdmazharkhan-1078,
Try the following suggestions:
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
I don't have an exact solution but do have code in a GitHub repository that should help. Although the code is in a windows form project the following classes can be used in any project type.
WindowsServices class
has methods to
Serviceinstaller class
has methods to