I recently wrote a program that needs to run on the server. After the compilation, I ran it on the server and found that it was okay to log in remotely to open the program. However, after ten minutes, if the remote server is automatically canceled, the program will be terminated. In this way, the program will not work normally. After thinking about the solution for more than a day, I finally solved the problem today.
First, write a service using. net.
Create a service, right-click on the design page, and select add installer after the menu appears. A new page is displayed, with the following controls: serviceProcessInstaller1 and serviceInstaller1.
Change the attribute Account to LocalSystem in serviceProcessInstaller1.
Change the attribute Parent to serviceProcessInstaller1 in serviceInstaller1. The ServiceName attribute is the name (set the name to gogo) after the service is generated)
After modifying the properties of the control. Return to the background of the new service page and add the following code:
Process p = new Process ();
P. StartInfo. FileName = "program path and file name ";
P. Start ();
After the token is completed, generate the token (fake name is w2.exe ). Find the generated EXE file in the corresponding folder, and find the two. EXE files with short names. Copy the file to the root directory of the drive F in a folder.
Install a service. Go to the cmd screen and enter the Framework2.0 file, for example:
Cd C: \ WINDOWS \ Microsoft. NET \ Framework \ v2.0.50727
After hitting
InstallUtil f: \ w2.exe. the uninstallation service is InstallUtil f: \ w2.exe-u.
Now you have to start the service.
Net start gogo, and the service starts. The service is opened, and the program that I run is opened by the System user, in this way, the program I need will not be terminated because the user is logged out. Then, the gogo service will end, because it only plays a transitional role, saves some resources for the server.