Handling Window Service Using Windows From [Resolved]

Posted by Nismeh under Windows Forms on 12/12/2013 | Points: 10 | Views : 1424 | Status : [Member] | Replies : 14
Hi,

I want to create an Windows App that can handle window services.

Means, I want to start stop window service using this application. Can you please provide me proper path to go ahead with it.

It will be great if you suggest me with full example or steps.

Thanks in advance
:)

IT KNOWLEDGE IS APPLIED KNOWLEDGE
So Just Do It



Responses

Posted by: vishalneeraj-24503 on: 12/12/2013 [Member] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
Import System.ServiceProcess for getting ServiceController class in windows application.

Now on start_button click,write:-

protected void start_button_click()
{

ServiceController service = new ServiceController(serviceName);
try
{
TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);

service.Start();
service.WaitForStatus(ServiceControllerStatus.Running, timeout);
}
catch(exception ex)
{
throw ex;
}
}


same for
protected void stop_button_click()
{
ServiceController service = new ServiceController(serviceName);
try
{
TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);

service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
}
catch(Exception ex)
{
throw ex;
}
}


Nismeh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: vishalneeraj-24503 on: 12/12/2013 [Member] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
Hi,you need to give reference of System.ServiceProcess.dll.

Clicking on Add reference and find System.ServiceProcess.dll.

Then you can import system.serviceprocess namespace.

Nismeh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: vishalneeraj-24503 on: 12/12/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Hi,refer below links:-

http://www.csharp-examples.net/restart-windows-service/
http://i-developer.blogspot.in/2010/11/start-stop-and-restart-windows-service.html
http://stackoverflow.com/questions/467367/how-can-i-programmatically-stop-start-a-windows-service-on-a-remote-box

Nismeh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: vishalneeraj-24503 on: 12/12/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Also refer:-
http://www.nullskull.com/q/10281653/restart-window-service-in-c.aspx
http://bhaskarrajukonduru-dotnet.blogspot.in/2010/06/how-to-start-and-stop-windows-services.html
http://social.msdn.microsoft.com/Forums/en-US/dbd5753d-f49b-4644-86fa-c2890e2d8efe/programmatically-starting-multiple-windows-services-in-c

Nismeh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Nismeh on: 12/12/2013 [Member] Starter | Points: 25

Up
0
Down
Hey Guys,

I know all these. I've gone through these links. But I want to do same thing via Button.
Now issue is if I am creating a win form than I am not able to add ServiceController as header file. And if I am adding this into same project and running it than, the project executes service rather than my form. This was the reason I asked for detailed steps or model for it.

IT KNOWLEDGE IS APPLIED KNOWLEDGE
So Just Do It

Nismeh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: vishalneeraj-24503 on: 12/12/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Please let me know if you are able to do.

Nismeh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Nismeh on: 12/12/2013 [Member] Starter | Points: 25

Up
0
Down
Hey Buddy,

Thanks for effort. But now Issue is I am not able to add that directory
 Download source file

IT KNOWLEDGE IS APPLIED KNOWLEDGE
So Just Do It

Nismeh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: vishalneeraj-24503 on: 12/12/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down

Follow the process as:-

Right-click the project in Solution Explorer and select Add Reference. On the .NET tab, scroll to System.ServiceProcess, select it, and click OK.

Nismeh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: vishalneeraj-24503 on: 12/12/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Please let me know,if it helps you and mark.

Nismeh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: vishalneeraj-24503 on: 12/12/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Hi Nismeh,

have you added reference?

Nismeh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Nismeh on: 12/12/2013 [Member] Starter | Points: 25

Up
0
Down
Hi Vishal,

Sorry Was out for Lunch. Thanks.. :)It worked. Actually I was so confused that adding reference didn't come in mind :P

Thanks again :)

IT KNOWLEDGE IS APPLIED KNOWLEDGE
So Just Do It

Nismeh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: vishalneeraj-24503 on: 12/12/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Welcome Nismeh.

Nismeh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Nismeh on: 12/12/2013 [Member] Starter | Points: 25

Up
0
Down
One more thing. I was thinking to pass argument while starting a service. Can you suggest me that Is it Possible or not?

IT KNOWLEDGE IS APPLIED KNOWLEDGE
So Just Do It

Nismeh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Nismeh on: 12/12/2013 [Member] Starter | Points: 25

Up
0
Down
Got It. No worries. :)

IT KNOWLEDGE IS APPLIED KNOWLEDGE
So Just Do It

Nismeh, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response