Run Windows Services on a remote computer

Source: Internet
Author: User

Recently I wrote a small program that runs on a remote server. However, when the system logs out the current login user, the program started by the user also stops. I checked a lot of information, it is not ideal to try to modify the registry and so on. simply do not be lazy. You can honestly write it as a Windows service. The procedure for creating a simple Windows service is as follows:

(1) create a Windows Service Project, right-click on the design page, and select add installer after the menu appears. A new page is displayed, with the controls serviceprocessinstaller1 and serviceinstaller1. In serviceprocessinstaller1, change the attribute account to LocalSystem, and change the attribute parent in serviceinstaller1 to serviceprocessinstaller1, the servicename attribute is the service name after the service is generated (for example, MSG ).

(2). After modifying the properties of this control. Return to the background of the new service page and add your own code:

(3 ). after the code is written, generate the file, find the generated EXE file under the project directory, and copy the file to the f disk. run cmd to enter the following directory: cd c:/Windows/Microsoft. net/framework/v2.0.50727, install this service: installutil F:/servicetest.exe. (The method to uninstall the service is installutil F:/servicetest.exe-U ).

(4) After the service is installed, start the service net start MSG (MSG is the service name), and a simple service is completed. The following code is provided:

 

Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. diagnostics;
Using system. serviceprocess;
Using system. text;
Using system. runtime. interopservices;
Using system. IO;

Namespace msgservice
...{
/** // <Summary>
/// Explain: Windows Service
/// Author: ymq
/// Createdate: 2008-04-02
/// Last modify:
/// </Summary>
Public partial class service1: servicebase
...{

Private system. Timers. Timer timersender;

Public service1 ()
...{
Initializecomponent ();
This. timersender = new system. Timers. Timer ();
This. timersender. interval = 120000; // 2 minutes
This. timersender. elapsed + = new system. Timers. elapsedeventhandler (timersender_elapsed );
}

// Start the service
Protected override void onstart (string [] ARGs)
...{
Try
...{
String strpath = appdomain. currentdomain. basedirectory; // relative path of the application
Streamwriter Sw = new streamwriter (@ strpath + @ "sendmsglog.txt", true, system. Text. encoding. Default );
Sw. Write (datetime. Now. tow.datestring () + "" + datetime. Now. tow.timestring () + "");
Sw. Close ();

This. timersender. Enabled = true; // enable cyclic transmission.
}
Catch (exception ex)
...{
String strpatherr = appdomain. currentdomain. basedirectory;
Streamwriter swerr = new streamwriter (@ strpatherr + @ "sendmsgerr.txt", true, system. Text. encoding. Default );
Swerr. Write (ex. Message + "");
Swerr. Close ();
}
}

Protected override void onstop ()
...{
// Todo: Add code here to stop the service.
}

// Sends information through cyclic Detection
Private void timersender_elapsed (Object sender, system. Timers. elapsedeventargs E)
...{
Streamwriter Sw = NULL;
Try
...{
String strpath = appdomain. currentdomain. basedirectory; // relative path of the application
Sw = new streamwriter (@ strpath + @ "sendmsglog.txt", true, system. Text. encoding. Default );
Sw. Write (datetime. Now. tow.datestring () + "" + datetime. Now. tow.timestring () + "Start :");
}
Catch (exception ex)
...{
String strpatherr = appdomain. currentdomain. basedirectory; // relative path of the application
Streamwriter swerr = new streamwriter (@ strpatherr + @ "sendmsgerr.txt ");
Swerr. Write (ex. Message + "");
Swerr. Close ();
}
Finally
...{
Sw. Close ();
}
}
}
}

 

 

 

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.