Using C # To create a Windows Service in. Net is actually very simple. You can follow the steps below to create a simple Windows Service.
1. Select Windows Service when creating the project, so that. Net will automatically generate the framework of Windows Service;
2. Complete the corresponding events of Windows Service, mainly OnStart and OnStop. After completing these events, the general code is as follows:
Using System;
Using System. Collections;
Using System. ComponentModel;
Using System. Data;
Using System. Diagnostics;
Using System. ServiceProcess;
Using System. IO;
Using System. Threading;
Namespace WinSDemo
{
Public class WinSDemo: System. ServiceProcess. ServiceBase
{
/// <Summary>
/// Required designer variable.
/// </Summary>
Private System. ComponentModel. Container components = null;
Private bool blnStopThread;
Private Thread thdMain;
Public WinSDemo ()
{
// This call is required by the Windows. Forms Component Designer.
InitializeComponent ();
// TODO: Add any initialization after the InitComponent call
}
// The main entry point for the process
Static void Main ()
{
System. ServiceProcess. ServiceBase [] ServicesToRun;
// More than one user Service may run within the same process. To add
// Another service to this process, change the following line
// Create a second service object. For example,
//
ServicesToRun = new System. ServiceProcess. ServiceBase [] {new WinSDemo ()};
System. ServiceProcess. ServiceBase. Run (ServicesToRun );
}
/// <Summary>
/// Required method for Designer support-do not modify
/// The contents of this method with the code editor.
/// </Summary>
Private void InitializeComponent ()
{
//
// WinSDemo
//
This. CanPauseAndContinue = true;
This. ServiceName = "MyTest ";
}
/// <Summary>
/// Clean up any resources being used.
/// </Summary>
Protected override void Dispose (bool disposing)
{
If (disposing)
{
If (components! = Null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}
/// <Summary>
/// Set things in motion so your service can do its work.
/// </Summary>
Protected override void OnStart (string [] args)
{
// TODO: Add code here to start your service.
ThdMain = new Thread (new ThreadStart (WriteLog ));
ThdMain. Start ();
}
/// <Summary>
/// Stop this service.
/// </Summary>
Protected override void OnStop ()
{
// TODO: Add code here to perform any tear-down necessary to stop your service.
BlnStopThread = true;
ThdMain. Join ();
}
Protected override void OnPause ()
{
ThdMain. Suspend ();
}
Protected override void OnContinue ()
{
ThdMain. Resume ();
}
Protected void WriteLog ()
{
StreamWriter myWriter = null;
Do
{
Process. Start ("net", "send localhost yourValue ");
Try
{
MyWriter = new StreamWriter ("c: // MyLog.txt", true );
MyWriter. WriteLine (DateTime. Now. ToString ());
MyWriter. Close ();
}
Catch {};
Thread. Sleep (5000 );
} While (blnStopThread = false );
}
}
}
Note: We recommend that you modify the Service name in InitializeComponent to enable you to better identify your own Windows Service.
3. in order to make the Service you write can be loaded into the system, the above steps are not enough. Next, add the Service Installer to the current project and set the start status after the Service is installed, the Code is as follows:
Using System;
Using System. Collections;
Using System. ComponentModel;
Using System. Configuration. Install;
Using System. ServiceProcess;
Namespace WinSDemo
{
/// <Summary>
/// Summary description for WinSDemoIns.
/// </Summary>
[RunInstaller (true)]
Public class WinSDemoIns: System. Configuration. Install. Installer
{
/// <Summary>
/// Required designer variable.
/// </Summary>
Private System. ComponentModel. Container components = null;
Private ServiceInstaller serviceInstaller;
Private ServiceProcessInstaller processInstaller;
Public WinSDemoIns ()
{
// This call is required by the Designer.
InitializeComponent ();
// TODO: Add any initialization after the InitComponent call
ProcessInstaller = new ServiceProcessInstaller ();
ServiceInstaller = new ServiceInstaller ();
// Service will run under system account
ProcessInstaller. Account = ServiceAccount. LocalSystem;
// Service will have Start Type of Manual
ServiceInstaller. StartType = ServiceStartMode. Automatic;
ServiceInstaller. ServiceName = "MyTest ";
Installers. Add (serviceInstaller );
Installers. Add (processInstaller );
}
# Region Component Designer generated code
/// <Summary>
/// Required method for Designer support-do not modify
/// The contents of this method with the code editor.
/// </Summary>
Private void InitializeComponent ()
{
Components = new System. ComponentModel. Container ();
}
# Endregion
}
}
4. after the preceding steps are completed, the code is compiled into an executable file and reused. net Service Installation tool, that is, in the Dos window, type "installutil yourService.exe" to execute this operation. On the contrary, if you want to uninstall the Service, add a parameter, that is, "installutil/u yourService.exe ". .Net的.
For future Service deployment.. Net program. The runtime environment must be installed... Net runtime environment.