Windows Service Tutorial 1
This tutorial is to build a simple Windows Services.
1. Create a Windows Service Project, File->New->Project
2. Please add these codes into Services1.cs
Code
protected override void OnStart(string[] args) | |
{ | |
EventLog.WriteEntry("Example Start"); | |
System.Timers.Timer timer1 = new System.Timers.Timer(); | |
timer1.Enabled = true; | |
timer1.Interval = 60000; | |
timer1.Elapsed += new System.Timers.ElapsedEventHandler(timer1_Elapsed); | |
timer1.Start(); | |
} | |
| |
void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e) | |
{ | |
EventLog.WriteEntry("Timer Triggered:"+DateTime.Now.ToString()); | |
} | |
| |
protected override void OnStop() | |
{ | |
EventLog.WriteEntry("Example Stop"); | |
} |
3. Go to the design view of Services1.cs, right click, add Installer
Then the ProjectInstaller.cs is generated.
4. Go to the design view of ProjectInstaller.cs, Select serviceProcessInstaller1 set the account to be LocalService
5. Right the solution Add->New Project
Under Other Project Types->Setup and Deployment->Visual Studio Installer->Setup Project
6. Right click Setup Project, Add a Project Output
7. Select ExampleWindowsServices
8. Right click Setup Project, View the Custom Actions
9. Add a custom action, this is to install the service into the Windows Service Catalog.
10. Select ExampleWindowsSerive
Then rebuild the solution and run the setup project. It will install the service into your computer
Please click here to download the source for this tutorial.
Reference:
Walkthrough: Creating a Windows Service Application in the Component Designer
Trackback address for this post
Trackback URL (right click and copy shortcut/link location)
Feedback awaiting moderation
This post has 763 feedbacks awaiting moderation...
Form is loading...