http://www.c-sharpcorner.com/uploadfile/mirfan00/windows-service-in-C-Sharp/
Introduction
This
article is about Windows Service in .Net Frame. We learn how to make
and use windows service in our C#.Net Frame applications.
Steps to create windows service
1.
Open new windows project in Visual Studio.
2. Right click on windows service screen and select add installer
Two installers added and displays on windows service design view.
3. Go to service installer property and sets the following properties
- Description
- Display Name
- Service Name
- Start Type
4. Go to service process installer property and sets the account property to 'Local System'
5. Now drag a tool of evenLog1 from tool box into service designer and write the following code:
public Service1()
{
InitializeComponent();
if (!System.Diagnostics.EventLog.SourceExists("MySource"))
System.Diagnostics.EventLog.CreateEventSource("MySource", "MyNewLog");
this.eventLog1.Source = "MySource";
this.eventLog1.Log = "MyNewLog";
}
On Start function
protected override void OnStart(string[] args)
{
this.eventLog1.WriteEntry("Windows service start at: " +
DateTime.Now.TimeOfDay);
}
On Stop function
protected override void OnStop()
{
this.eventLog1.WriteEntry("Windows service stop at: " +
DateTime.Now.TimeOfDay);
}
6. Add setup project to install our service on system.
7. Add project output into setup project. Right click on setup project and select add-> project output.
8. Right click on setup project and select view->custom action.
9. Right click on custom action that opens in new window and add custom action.
10. Select application Folder and add primary output from windows service and then click ok.
Project output added successfully.
11. Rebuild
these two projects (windows service & setup) and now right click on
setup, select install option. Windows service is installed on your
system successfully.
First time you have to
start the service manually or you may restart your system
System Services Screen
You can check whether your service is working or not, just view system event viewer from control panel(administrative tools).
On start even log
On stop event log
Two installers added and displays on windows service design view.
3. Go to service installer property and sets the following properties
- Description
- Display Name
- Service Name
- Start Type
4. Go to service process installer property and sets the account property to 'Local System'
5. Now drag a tool of evenLog1 from tool box into service designer and write the following code:
public Service1()
{
InitializeComponent();
if (!System.Diagnostics.EventLog.SourceExists("MySource"))
System.Diagnostics.EventLog.CreateEventSource("MySource", "MyNewLog");
this.eventLog1.Source = "MySource";
this.eventLog1.Log = "MyNewLog";
}
On Start function
protected override void OnStart(string[] args)
{
this.eventLog1.WriteEntry("Windows service start at: " +
DateTime.Now.TimeOfDay);
}
On Stop function
protected override void OnStop()
{
this.eventLog1.WriteEntry("Windows service stop at: " +
DateTime.Now.TimeOfDay);
}
6. Add setup project to install our service on system.
7. Add project output into setup project. Right click on setup project and select add-> project output.
8. Right click on setup project and select view->custom action.
9. Right click on custom action that opens in new window and add custom action.
10. Select application Folder and add primary output from windows service and then click ok.
Project output added successfully.
11. Rebuild
these two projects (windows service & setup) and now right click on
setup, select install option. Windows service is installed on your
system successfully.
First time you have to
start the service manually or you may restart your system
System Services Screen
You can check whether your service is working or not, just view system event viewer from control panel(administrative tools).
On start even log
On stop event log
4. Go to service process installer property and sets the account property to 'Local System'
5. Now drag a tool of evenLog1 from tool box into service designer and write the following code:
public Service1()
{
InitializeComponent();
if (!System.Diagnostics.EventLog.SourceExists("MySource"))
System.Diagnostics.EventLog.CreateEventSource("MySource", "MyNewLog");
this.eventLog1.Source = "MySource";
this.eventLog1.Log = "MyNewLog";
}
public Service1()
{
InitializeComponent();
if (!System.Diagnostics.EventLog.SourceExists("MySource"))
System.Diagnostics.EventLog.CreateEventSource("MySource", "MyNewLog");
this.eventLog1.Source = "MySource";
this.eventLog1.Log = "MyNewLog";
}
public Service1()
{
InitializeComponent();
if (!System.Diagnostics.EventLog.SourceExists("MySource"))
System.Diagnostics.EventLog.CreateEventSource("MySource", "MyNewLog");
this.eventLog1.Source = "MySource";
this.eventLog1.Log = "MyNewLog";
}
public Service1()
{
InitializeComponent();
if (!System.Diagnostics.EventLog.SourceExists("MySource"))
System.Diagnostics.EventLog.CreateEventSource("MySource", "MyNewLog");
this.eventLog1.Source = "MySource";
this.eventLog1.Log = "MyNewLog";
}
|
On Start function
On Start function
On Start function
On Start function
On Start function
protected override void OnStart(string[] args)
{
this.eventLog1.WriteEntry("Windows service start at: " +
DateTime.Now.TimeOfDay);
}
protected override void OnStart(string[] args)
{
this.eventLog1.WriteEntry("Windows service start at: " +
DateTime.Now.TimeOfDay);
}
protected override void OnStart(string[] args)
{
this.eventLog1.WriteEntry("Windows service start at: " +
DateTime.Now.TimeOfDay);
}
protected override void OnStart(string[] args)
{
this.eventLog1.WriteEntry("Windows service start at: " +
DateTime.Now.TimeOfDay);
}
|
On Stop function
On Stop function
On Stop function
On Stop function
On Stop function
protected override void OnStop()
{
this.eventLog1.WriteEntry("Windows service stop at: " +
DateTime.Now.TimeOfDay);
}
protected override void OnStop()
{
this.eventLog1.WriteEntry("Windows service stop at: " +
DateTime.Now.TimeOfDay);
}
protected override void OnStop()
{
this.eventLog1.WriteEntry("Windows service stop at: " +
DateTime.Now.TimeOfDay);
}
|
6. Add setup project to install our service on system.
6. Add setup project to install our service on system.
7. Add project output into setup project. Right click on setup project and select add-> project output.
7. Add project output into setup project. Right click on setup project and select add-> project output.
8. Right click on setup project and select view->custom action.
8. Right click on setup project and select view->custom action.
9. Right click on custom action that opens in new window and add custom action.
9. Right click on custom action that opens in new window and add custom action.
10. Select application Folder and add primary output from windows service and then click ok.
10. Select application Folder and add primary output from windows service and then click ok.
Project output added successfully.
Project output added successfully.
11. Rebuild
these two projects (windows service & setup) and now right click on
setup, select install option. Windows service is installed on your
system successfully.
First time you have to
start the service manually or you may restart your system
System Services Screen
You can check whether your service is working or not, just view system event viewer from control panel(administrative tools).
On start even log
On stop event log
System Services Screen
Comments
Post a Comment