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


Top 5 Contributors of the Month
david stephan

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#
Hi,
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!!!

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

Arpita,


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);
thread.Start();
}

public void Actions()
{
Thread.Sleep(500000);
}
}
}


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;

[RunInstaller(true)]
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";
this.Installers.Add(serviceProcessInstaller);
this.Installers.Add(serviceInstaller);
}
}


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
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

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