C # Writing Windows services and making installation packages

Source: Internet
Author: User

There are many stages in understanding the service.

The first stage: in C + +, know in a process while (True) {}, and then do a lot of things inside, this is called the service, the interface may also use Console console program.

The second stage: know that the service to perform in the background, generally will not have the interface display, and boot from the boot, and then hide the console console interface, and by using SC CREATE will exe into a service, and to achieve the purpose of boot. This is also the C + + phase.

The third stage: because the project needs to contact C #, found that there is a project template specifically for the production of Windows services, try to, feel good, save a lot. Install the service from the command line.

Stage four: Found a good thing to make the installation package, eliminating the pain of knocking on the command line, for the implementation of personnel is certainly a gospel

One, write Windwos service

C # Writing a Windows service is fairly straightforward, at least simpler than C + +.

1, create a new project for Windows services. Fill in the service name Testservice

Created successfully, the project interface is like this

2, write the service code, right-Testservice, view the code OnStart, write the startup code. OnStop Writing a stop code

namespacetestservice{ Public Partial classtestservice:servicebase {FileStream fs=NULL; StreamWriter SW=NULL; Thread TD=NULL;  PublicTestservice () {InitializeComponent (); }        protected Override voidOnStart (string[] args) {TD=NewThread (Workthread); Td.        Start (); }        protected voidWorkthread () {FS=NewFileStream (@"F://ggg.txt", FileMode.OpenOrCreate, FileAccess.Write); SW=NewStreamWriter (FS);  while(true) {SW.                WriteLine (DateTime.Now.ToString ()); Sw.                                Flush (); Thread.Sleep ( -); }        }        protected Override voidOnStop () {if(TD! =NULL) {TD.            Abort (); } SW.            Close (); Fs.                    Close (); }    }}

3, double-click TestService.cs, right in the right margin to add the installer, there will be two things, click ServiceInstall1 to modify the name of the service, as well as the startup type. Click ServiceProcessinstall1 to change account to LocalSystem.

4,ok is done, a simple service has been completed. Next, we test whether the service can run normally. Right-click the administrator to open the command Prompt box, data installutil testservice (service name) to install the service, smoothly, there will be the following prompts to complete the character. At this point you will see the F-disk flicker ggg.txt writing files.

  second, the production of installation packages ,

Start making the installation package, we can not take the service out every time with the command line to install, not very friendly. Right-click the solution, in other types of installation deployment, if found not installed, first go to install this tool, very simple, vsi_bundle.exe only 5 m size.

Right-click on the Application folder to add the project output.

Right-click Project-View-Add custom action-right-click to add a custom action

At this point, recompiling the Setup project will generate Setup.exe, which is our service installation package. This is where the entire service authoring package is completed.

Good luck

C # Writing Windows services and making installation packages

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.