C # launches external programs through Windows services

Source: Internet
Author: User

1. Create a new Windows service application

Create Project--"+" on the left side of Visual C #--"windows--" Windows service (right template)--Enter a name to determine the creation of the project

2. Set the properties of the Windows service (there are no forms in the Windows service, so click on the left-hand side of the designer to see the properties in the property bar on the right)

The property here is to control whether the server can stop, pause, continue, and so on. Select as needed. The name of the property is well understood and does not require much explanation. I set the CanPauseAndContinue and CanShutdown to True.

The ServiceName property is the name that is displayed in the Service Manager later in this service.

3. Setting up the installation program for Windows services

In the design window, right-click the mouse and select Add Installer. This allows you to add ProjectInstaller.cs and both the ServiceInstaller1 and ServiceProcessInstaller1 objects have a more important property.

Click ServiceInstaller1 in the Properties window on the right to have the StartType property, specifying how and when to start the service. Set this property to Automatic if you want the service to boot and keep running.

Click ServiceProcessInstaller1 in the Properties window on the right to have the Account property, which specifies the type of accounts running this service. If you want to use this service for all users, set this property to LocalService

At this time, all creation and property settings work is over. Here's how to start coding.

4. A timer is required to periodically detect whether the program is running, so we will add a Timer control.

Open the Code window.

First, increase the using System.Timers; Reference.

There are two functions of onstart (string[] args) and OnStop (). As the name implies, OnStart is the code that runs when the service starts. OnStop is the code that runs at the end of the service.

We create a Timer control in OnStart and set its properties and increment events.

System.Timers.Timer t = new System.Timers.Timer (1000); The 1000 here refers to a timer with a time interval of 1000 milliseconds.

t.elapsed + = new System.Timers.ElapsedEventHandler (Timer_click); Timer_click is the function that executes the event when the time arrives

T.autoreset = true; Whether the setting is executed once (false) or always executed (true)

T.enabled = true; Whether to perform the System.Timers.Timer.Elapsed event

This will execute the Timer_click function every 1 seconds after the service is run

5. Writing the Timer_click function

private void Timer_click (Object sender, Elapsedeventargs e)
{

process[] Localbyname = Process.getprocessesbyname ("360tray.exe");

The 360tray.exe here is the name of the process for the program you want to execute. is basically the file name of the. exe file.

Localbyname gets all the processes with the name "360tray.exe" in the process.
if (Localbyname.length = = 0)//If the number of processes received is 0, then the program does not start, you need to start the program
{
Process.Start ("C://360tray.exe"); Startup program "C://360tray.exe" is the path of the program
}

Else

{

If the program is already started, execute this part of the code

}
}

Now all the code work is done. The function achieved is to detect if the 360tray.exe is running every second after the service is started. If it is not running, start 30tray.exe. If it is already running, no action is taken.

6. Applications that build Windows services

Click Generate--Generate WindowsService1

In this case, the c:/my documents/visual Studio 2005/projects/windowsservice1/windowsservice1/bin/debug folder will generate WindowsService1.exe file.

If successful, the next step can be performed.

7. Installation Services

Unlike applications, services cannot be run in Visual Studio and must be installed in the Windows service using the installation software.

This installation software is in the c:/windows/microsoft.net/framework/v2.0.50727 folder.

Program name is InstallUtil.exe

To run this program, you need to use the command prompt program.

Click Start-Run--enter "CMD" to click on OK

Enter CD c:/windows/microsoft.net/framework/v2.0.50727 Enter this folder

Input InstallUtil c:/my documents/visual Studio 2005/projects/windowsservice1/windowsservice1/bin/debug/ WindowsService1.exe is the address of the InstallUtil + service. exe file. The service is now installed.

If you want to uninstall this service also use this software.

Input InstallUtil c:/my documents/visual Studio 2005/projects/windowsservice1/windowsservice1/bin/debug/ Windowsservice1.exe-u is the "InstallUtil + service. exe file Address-u" carriage return. Then the service is uninstalled.

Of course we don't need to uninstall this service right now. Now it's only the last step to realize our function.

8. Start the service

After the service installation is successful, it does not start, we need to start it in Service Manager, and if the program you are running is a program with a form, you also need to modify the properties of the service.

Go to Control Panel--"Management tools--" service opens the service Manager.

Find the service we just installed in the list of services on the right, the service name is the content of the ServiceName property in step 2nd. This is WindowsService1.

If the program you are running is a program with a form, right-click the service Select Properties-click the Login tab (at the top)-Select Local System account-tick the "Allow service to interact with desktop" check box-click OK to exit the property.

This is where the program with the form will run normally. Otherwise you will only see the program in the process, but there is no form.

You can now click on the service and click Start to start the service.

The whole process is over.

C # launches external programs through Windows services

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.