Monday 12 September 2011

MyService Demo

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
namespace  ServiceDemo
{
partial class MyFirstWindowService : ServiceBase{
public MyFirstWindowService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service.using (Process process = new Process())
{
process.StartInfo = new ProcessStartInfo("calc.exe");
process.StartInfo.CreateNoWindow = true;
process.StartInfo.ErrorDialog = false;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
process.Start();
System.Threading.Thread.Sleep(10000);
if (!process.HasExited)
{
process.WaitForExit(120000);
if (!process.HasExited)
process.Kill();
}
}
}
protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to stop your service.}
}
}

using
System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.ServiceProcess;
using System.Linq;

namespace
  ServiceDemo
{
[RunInstaller(true)]
public partial class MyWindowServiceInstaller : Installer{
public MyWindowServiceInstaller()
{
InitializeComponent();
ServiceProcessInstaller serviceProcessInstaller =
new ServiceProcessInstaller();
ServiceInstaller serviceInstaller = new ServiceInstaller();
//# Service Account InformationserviceProcessInstaller.Account = ServiceAccount.LocalSystem;
serviceProcessInstaller.Username = null;
serviceProcessInstaller.Password = null;
//# Service InformationserviceInstaller.DisplayName = "My New C# Windows Service";
serviceInstaller.StartType = ServiceStartMode.Automatic;
//# This must be identical to the WindowsService.ServiceBase name//# set in the constructor of WindowsService.csserviceInstaller.ServiceName = "My Windows Service";
this.Installers.Add(serviceProcessInstaller);
this.Installers.Add(serviceInstaller);
}
}
}

using
System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;
namespace ServiceDemo
{
static class Program{
/// <summary>/// The main entry point for the application./// </summary>static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new MyFirstWindowService()
};
ServiceBase.Run(ServicesToRun);
}
}
}