Window service used in recent projectsProgramI have never touched on it before. I am a stranger. I spent a whole day learning about it and wrote a simple service. But when I made the installation program, I referred to a lot of information on the Internet, however, they cannot be created successfully. It may be because the development environment or project configuration is different. Here, we will post the method we have created successfully. On the one hand, we will leave it to ourselves, on the one hand, we hope that we can be able to find ourselves in despair and lose the soul of passers-...
1. Create a window Service Project
First, create a window Service Project: solution (right-click) --> Add --> Create a project --> select windows as the project type --> Windows service,
2. Compile the window service program
After creation, two files, program. CS and service1.cs, are generated automatically. We add the required operations to service1.cs. There are two rewrite Methods onstart and onstop, onstart indicates the operation to start the service, and onstop indicates the operation to stop the service. Below is a simple example: after the service is started, information is recorded in a notepad file every second. The Code is as follows:
Using System; Using System. Collections. Generic; Using System. componentmodel; Using System. Data; Using System. diagnostics; Using System. LINQ; Using System. serviceprocess; Using System. text; Using System. IO; Using System. Threading; Namespace Windowsservice2010 { Public Partial Class Service1: servicebase { Public Service1 () {initializecomponent ();} system. Timers. Timer time2 = New System. Timers. Timer ( 1000 ); Protected Override Void Onstart ( String [] ARGs) {time2.elapsed + = New System. Timers. elapsedeventhandler (timers_elapsed); time2.enabled = True ;} Protected Override Void Onstop () {time2.enabled = False ;} Private Void Timers_elapsed ( Object Sender, system. Timers. elapsedeventargs e ){ String Path = " D: \ textfile.txt " ; Streamwriter SW; If (!File. exists (PATH) {SW = File. createtext (PATH );} Else Sw = File. appendtext (PATH); Sw. writeline ( " ============================================ " ); Sw. Write ( " Date added: " + Datetime. Now. tostring () + " \ R \ n " ); Sw. Write ( " The log Content is: " + " Test Service " + " \ R \ n " ); Sw. writeline ( " ============================================ " ); Sw. Flush (); Sw. Close ();}}}
At this point, our services and functions implemented by the services have been written. How can we install our services on the client? First, add an installer class, switch to the View window, right-click --> add installer, and a program installation class file projectinstaller. CS will be added,
Here, serviceprocessinstaller1 and serviceinstaller1 have several important attributes to be set. starttype in serviceinstaller1 must be set to automatic, indicating random start, servicename indicates service name, description indicates service description, and displayname indicates display name.
In serviceprocessinstaller1, the account (Account type used to run this service) must be set to LocalSystem, indicating the local system account. In this way, the service will always start no matter which user logs on to the system.
Iii. Windows Service Installation
1. Run the following command: My computer-Attachments-command prompt-right-click the Administrator to run:
1.1. Installation
Run the installutil.exe service path in the Command window to install the window service. This command does not look very long. Here we will teach you how to look at it: This command is divided into three parts: Previous paragraph: path of. NET Framework 4.0 installed on drive C under windows. Middle Section: a space. Later: the path of the window service to be installed after the release. The above paths can be opened directly on the computer, complex, directly pasted in the command symbol. 1.2 uninstall, start, and stop. You can enter the following command in the command line: "SC" contains many syntaxes for operations. Uninstall service: SC Delete 'service name' start service: SC start' service name' stop service: SC stop' service name' 2. create an installer: solution (right-click) --> Add --> Create a project --> select "Install and deploy" for the project type --> select "install project" for the template: After creation, installation Project (right-click) --> View --> File System
Because we are installing the service, you do not need to use the desktop and program menu. You can directly use the application folder (right-click) --> Add --> project output graph:
There are two points to note: select the service to be installed from the first project selection, select the main output from the second project selection, and click OK. In this step, the installer folder is added, the next step is to add operations for this installer. Here we add two basic operations: Install and uninstall. Installation Project (right-click) --> View --> Custom operation
We can see the installation, submission, rollback, uninstallation, and other operations above. We will first add the installation operation and install it (right-click) --> Add a custom operation. A dialog will pop up.
Select the application folder, select the previously added primary output item, and click OK. In this case, the installation operation is added for this installer, And the uninstallation operation is also added in this way, the only difference between uninstallation and installation is that you need to set a command parameter,
Here, input/u in arguments to indicate that the uninstall command is equivalent to the installutil.exe/u service path. Here, our installation program has been created, and the installation program project will be generated. setup.exe and Setup will be generated. if you click setup.exe to install our service like QQ, it is actually much better than QQ. QQ has advertisements. We are pure green!
In this way, we recommend that you use the first "command operator operation", and the second test. There is a problem during installation, which has not been solved yet. As the project is urgent, hurry up, huh, huh. If anyone is successful, please ask.