Link: http://www.cnblogs.com/yesun/archive/2007/06/16/431242.html
1. Create a Service Project
For example
Using system;
Using system. collections;
Using system. componentmodel;
Using system. Data;
Using system. diagnostics;
Using system. serviceprocess;
Using system. Threading;
Using system. Windows. forms;
Namespace catcharticle
{
Public class catcharticle: system. serviceprocess. servicebase
{
/// <Summary>
/// Required designer variables.
/// </Summary>
Private system. componentmodel. Container components = NULL;
Private thread mainthread;
Public catcharticle ()
{
// This call is required by the windows. Forms component designer.
Initializecomponent ();
Mainthread = new thread (New threadstart (threadfunc ));
Mainthread. Priority = threadpriority. Lowest;
}
// Master entry point of the process
Static void main ()
{
System. serviceprocess. servicebase [] servicestorun;
Servicestorun = new system. serviceprocess. servicebase [] {New catcharticle ()};
System. serviceprocess. servicebase. Run (servicestorun );
}
/// <Summary>
/// The designer supports the required methods-do not use the code editor
/// Modify the content of this method.
/// </Summary>
Private void initializecomponent ()
{
Components = new system. componentmodel. Container ();
This. servicename = "catcharticle ";
}
/// <Summary>
/// Clear all resources in use.
/// </Summary>
Protected override void dispose (bool disposing)
{
If (disposing)
{
If (components! = NULL)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}
/// <Summary>
/// Set specific operations so that the service can perform its work.
/// </Summary>
Protected override void onstart (string [] ARGs)
{
Mainthread. Start ();
}
/// <Summary>
/// Stop the service.
/// </Summary>
Protected override void onstop ()
{
Mainthread. Abort ();
}
Public static void threadfunc ()
{
Int lasthour = datetime. Now. hour;
While (true)
{
System. Threading. thread. Sleep (1000 );
If (datetime. Now. Hour-1 = lasthour)
{
MessageBox. Show ("to take good care of your eyes, please take a 5-minute break and look into the distance! "," Warning ", messageboxbuttons. OK, messageboxicon. Warning, messageboxdefaultbutton. button1, messageboxoptions. defaulttoptoponly );
Lasthour = datetime. Now. hour;
}
}
}
}
}
==========================================================
2. Add a project install. CS inherited from the installer. Some parameters can be defined by yourself.
Using system. collections;
Using system. configuration. Install;
Using system. serviceprocess;
Using system. componentmodel;
Namespace catcharticle
{
/// <Summary>
/// Summary of myinstall.
/// </Summary>
///
[Runinstaller (true)]
Public class myinstall: Installer
{
Private serviceinstaller;
Private serviceprocessinstaller processinstaller;
Public myinstall ()
{
Processinstaller = new serviceprocessinstaller ();
Serviceinstaller = new serviceinstaller ();
Processinstaller. Account = serviceaccount. LocalSystem;
Serviceinstaller. starttype = servicestartmode. Automatic;
Serviceinstaller. servicename = "catcharticle ";
Installers. Add (serviceinstaller );
Installers. Add (processinstaller );
}
}
}
3. Use release
4. Use c: \... \ framework \ v1.1.. \ installutil.exe c: \ Project path \ bin \ release \ testservice.exe
NOTE: If projectinstall. CS is not added, the following error is reported:
Installing the assembly "F: \ website directory \ edzh.com \ windowsservice \ bin \ debug \ windowsservice.exe ".
The affected parameters are:
Assemblypath = F: \ website directory \ edzh.com \ windowsservice \ bin \ debug \ windowsservice.exe
Logfile = F: \ website directory \ edzh.com \ windowsservice \ bin \ debug \ windowsservice. installlog
There is no public installer for runinstallerattribute. The "yes" attribute may be found in the F: \ website directory \ edzh.com \ windowsservice \ bin \ debug \ windowsservice.exe program.
Submitting the assembly "F: \ website directory \ edzh.com \ windowsservice \ bin \ debug \ windowsservice.exe ".
The affected parameters are:
Assemblypath = F: \ website directory \ edzh.com \ windowsservice \ bin \ debug \ windowsservice.exe
Logfile = F: \ website directory \ edzh.com \ windowsservice \ bin \ debug \ windowsservice. installlog
There is no public installer for runinstallerattribute. The "yes" attribute may be found in the F: \ website directory \ edzh.com \ windowsservice \ bin \ debug \ windowsservice.exe program.
No installer, so the installstate file is removed.
Installing the assembly "F: \ website directory \ edzh.com \ windowsservice \ bin \ debug \ windowsservice.exe ".
The affected parameters are:
Assemblypath = F: \ website directory \ edzh.com \ windowsservice \ bin \ debug \ windowsservice.exe
Logfile = F: \ website directory \ edzh.com \ windowsservice \ bin \ debug \ windowsservice. installlog
There is no public installer for runinstallerattribute. The "yes" attribute may be found in the F: \ website directory \ edzh.com \ windowsservice \ bin \ debug \ windowsservice.exe program.
Submitting the assembly "F: \ website directory \ edzh.com \ windowsservice \ bin \ debug \ windowsservice.exe ".
The affected parameters are:
Assemblypath = F: \ website directory \ edzh.com \ windowsservice \ bin \ debug \ windowsservice.exe
Logfile = F: \ website directory \ edzh.com \ windowsservice \ bin \ debug \ windowsservice. installlog
There is no public installer for runinstallerattribute. The "yes" attribute may be found in the F: \ website directory \ edzh.com \ windowsservice \ bin \ debug \ windowsservice.exe program.
No installer, so the installstate file is removed.