.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Sandeep Singh

Home >> Forum >> C# >> Post New QuestionBookmark and Share Subscribe to Forum

how to call a windows service?

Posted By: arpita     Posted Date: November 19, 2010    Points:2   Category :C#
I have completed a windows service application for clearing the temp files and prefetch files.I build the application successfully.
Now I to call the service against a button click event.
Can any body please tell me how to call the windows service against a button click event?

Please help!!!

Author: Sadhuryan             
Posted Date: November 19, 2010     Points: 5   


Normally you can manage these windows services within your services management console, we can start/stop/pause etc these services.

The basic structure of a service within the .net realm using C#, looks something like this:

using System.ServiceProcess;
using System.Threading;

namespace myWebservice
class Service : ServiceBase
static void Main()
ServiceBase.Run(new Service());

public Service()
Thread thread = new Thread(Actions);

public void Actions()

To deploy/install this service to a server/pc, create a class inherting from the installer class (available within the System.Configuration.Install assembly) like this:

using System.ComponentModel;
using System.Configuration.Install;
using System.ServiceProcess;

public class myServiceInstaller : Installer
public myServiceInstaller()
ServiceProcessInstaller serviceProcessInstaller = new ServiceProcessInstaller();
ServiceInstaller serviceInstaller = new ServiceInstaller();
serviceProcessInstaller.Account = ServiceAccount.LocalSystem;
serviceInstaller.DisplayName = "myService";
serviceInstaller.StartType = ServiceStartMode.Automatic;
serviceInstaller.ServiceName = "myService";

You can create a .net installer app to deploy the service or simply install it via a batch file running the following commands:

SET PATH=%path%;%windir%\Microsoft.NET\Framework\v2.0.50727 

REM Uninstall the Service if it exists already
InstallUtil /u myWebservice.exe

REM Install the Service
InstallUtil /i myWebservice.exe

REM Start the Service once we're done
net start myService PAUSE

In your case, you have to create windows application with a button which would invoke main/start method of your service

Post Reply

You must Sign In To post reply
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend