The example in this article describes how C # starts a program by creating a Windows service. Share to everyone for your reference, specific as follows:
1. Create a new Windows service application
Create Project--"+" on the left side of Visual C #--"windows--" Windows service (right side template)--Enter a name to determine the creation of the project
2. Set the properties of the Windows service (there is no form in the Windows service, so click the blank space in the left designer to see the properties in the right-hand property bar)
This property is the operation that controls whether the server can stop, pause, continue, and so on. Choose according to your needs. The name of the attribute is already clear and does not need to be explained too much. I set canpauseandcontinue and CanShutdown to True.
The ServiceName property is the name that is displayed in the later Service manager of this service.
3. Set up the Windows Service installation program
Right-click in the Design window and choose Add Installer. This allows you to add ProjectInstaller.cs as well as ServiceInstaller1 and ServiceProcessInstaller1 both objects have a more important attribute.
Clicking ServiceInstaller1 has a StartType property in the property window on the right, specifying how and when to start the service. If you want the service to boot up and run all the time, set this property to Automatic.
Click ServiceProcessInstaller1 has the account attribute in the property window to the right, specifying the type of accounts on which this service is running. If you want to have all users use this service, set this property to LocalService
All the creation and property setup work is over. The following starts with code writing.
4. Timer is required to detect whether the program is running, so we want to add a Timer control.
Open the Code window.
First, increase the using System.Timers; Reference.
Here are OnStart (string[] args) and onstop () two functions. As the name suggests, OnStart is the code that runs when the service starts. OnStop is the code that runs when the service ends.
We create the timer control in OnStart and set its properties and add events.
System.Timers.Timer t = new System.Timers.Timer (1000); Here the 1000 refers to the Timer interval of 1000 milliseconds
t.elapsed + = new System.Timers.ElapsedEventHandler (Timer_click);//timer_ Click is the function that executes the event at the time of arrival
T.autoreset = true;//Set to execute once (false) or Always (true)
t.enabled = true; Whether to perform System.Timers.Timer.Elapsed events
In this way, the Timer_click function is executed every 1 seconds after the service is run
5. Write the Timer_click function
private void Timer_click (Object sender, Elapsedeventargs e)
{
process[] Localbyname = Process.getprocessesbyname ("EXE");
if (! Isexistprocess ("EXE"))///If the number of processes is 0, then the program is not started, you need to start the program
{
process.start ("EXE");//Start the program's path
}
Else
{
//If the program is already started, execute this part of the code}
}
private bool Isexistprocess (string processname)
{
process[] myprocesses = process.getprocesses ();
foreach (Process myprocess in myprocesses)
{
if (MyProcess.ProcessName.CompareTo (processname) = 0)
{ C19/>return true;
}
}
return false;
}
Now all the code work is done.
6. Applications that generate Windows services
Click Build--"Generate WindowsService1
In this case, the WindowsService1.exe file is generated in the D:\Soft\WindowsService1\bin\Debug folder.
If successful, you can proceed to the next step.
7. Installation Services
Set in the ServiceInstaller1 property:
Description (Description of system services)
DisplayName (name displayed in system services)
ServiceName (source name in application event in System Event Viewer)
ServiceProcessInstaller1 property setting: Account pull down set to LocalSystem
Services, unlike applications, cannot be run in Visual Studio and must be installed into Windows services using the installation software. After completing the three screenshots above, continue with the following registration and uninstall operation.
This installation software is in the C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319 folder.
Program name is InstallUtil.exe
To run this program, you need to use the command prompt program.
Click Start--"Run-" Enter "cmd" click OK
Enter CD C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319 Enter this folder
Enter the address return of the InstallUtil D:\Soft\WindowsService1\bin\Debug\WindowsService1.exe that is InstallUtil + service. exe file. At this point the service is installed.
This software is also used if you want to uninstall this service.
Enter InstallUtil D:\Soft\WindowsService1\bin\Debug\WindowsService1.exe-u that is, "InstallUtil + service. exe file's address-u" carriage return. Then the service is unloaded.
Of course we do not need to uninstall this service now. Now only the last step to achieve our function.
8. Start Service
After the service installation was successful, it did not start, we need to start it in the service manager, and if the program running is a program with a form, you need to modify the properties of the service.
Go to Control Panel--"admin tools-" Service open Service Manager. or run-------->cmd------->services.msc---Local Service settings
In the list of services on the right, locate the service that we just installed, and 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 and select Properties--click on the "Login" tab (top)--Select the local System account--on the Allow service to interact with Desktop check box-click OK to exit the attribute.
This is, the program with the form will not function correctly. Otherwise, the program will only be seen in the process, but not the form.
You can now click the service, and then click Start to start the service.
The whole process is over.
You can also create an installation for the service!
Read more about C # Interested readers can view the site topics: "C # traversal algorithm and Skills summary", "C # Design Thread usage Tips", "C # Operation Excel Skills Summary", "C # XML file Operation Tips Summary", "C # Common control usage Tutorial", " WinForm Control Usage Summary, C # tutorial on data structure and algorithms, C # array operation techniques Summary, and C # Introduction to object-oriented Programming
I hope this article will help you with C # programming.