VS2010 Creating a Windows service Program

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.