Refer to the Online Protection eye program, the self-writing program is as follows.
1. Create an application with a noun of "careeyeservice" and a type of "Windowsservice".
Automatically generated code such as:
2, modify the ServiceCareEye.cs code
public partial class Servicecareeye:servicebase
{
Private Thread Mainthread;
Public Servicecareeye ()
{
InitializeComponent ();
Mainthread = new Thread (new ThreadStart (Thredfunc));
mainthread.priority = Threadpriority.lowest;
}
protected override void OnStart (string[] args)
{
Mainthread.start ();
}
protected override void OnStop ()
{
if (mainthread.threadstate = = System.Threading.ThreadState.Running)
{
Mainthread.abort ();
}
}
public void Thredfunc ()
{
int lasthour = DateTime.Now.Hour;
while (true)
{
Thread.Sleep (60000);
if (datetime.now.hour-1 = = Lasthour)
{
MessageBox.Show ("For the sake of your eye's health, please close your eyes and rest for 5 minutes or overlook 5 minutes", "warning",
MessageBoxButtons.OK, Messageboxicon.exclamation, Messageboxdefaultbutton.button1);
Lasthour = DateTime.Now.Hour;
}
}
}
}
3, modify the Design view of Servicecareeye service, right click "Add Installer",
4, in order to set the service login password, if you do not want to set the user name password can also use the Local System account to run the service, the code is as follows
This.serviceInstaller1.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
This completes the creation.
Installing and uninstalling Windows Services
After the finished Windows service is published is an EXE file, in order to enable the service on the machine used, we will use the tools provided by Microsoft InstallUtil Tool, the command line to install and uninstall the service.
InstallUtil tools in the directory: System disk: \windows\microsoft.net\framework\v4.0.30319, run cmd, input: C:\WINDOWS\Microsoft.NET\Framework\ V4.0.30319\installutil Xxxx.exe Enter to complete the installation of the Windows service.
The unload is the input c:\windows\microsoft.net\framework\v4.0.30319\installutil/u xxxx.exe carriage return.
When you run the Windowservice service after installation, you may sometimes encounter a situation where service is automatically down, in which case the EXE file at compile time is x64 or x86, so we use Mubuild to manually build into AnyCPU EXE service , it may be resolved.
Instance:
C:\windows\microsoft.net\framework\v4.0.30319>installutil.exe E:\congrixu\demo\
Careeyeservice\careeyeservice\bin\debug\careeyeservice.exe
VS2010 Creating a Windows service Program