The previous article introduced the use of templates to create, install, and uninstall Windows Services, this article continues to study this topic, but to create a console program , using Windows.TaskSchedule.exe installation .
Why use a console program? --Simple and rough commissioning. Ha ha.
After creating the console Application project, we edit the service principal code, and we continue to write a sentence to the text file.
Before implementing the specific logic, we need to refer to Windows.TaskSchedule.Extends.dll, the service class inherits the IJob .
1 usingSystem;2 usingSystem.Configuration;3 usingSystem.IO;4 usingWindows.TaskSchedule.Extends;5 6 namespaceMywindowstaskschedule7 {8 /// <summary>9 ///Concrete Service Logic implementationTen /// </summary> One Public classTest:ijob A { - Public voidExcute () - { the //throw new NotImplementedException (); -FileStream fs =NewFileStream (configurationmanager.appsettings["LogFile"]. ToString (), FileMode.OpenOrCreate, FileAccess.Write); -StreamWriter SW =NewStreamWriter (FS); -Sw. Basestream.seek (0, seekorigin.end); +Sw. WriteLine (string. Format ("\ "Mywindowstaskschedule\" Windows Service Run at {0} \ n", DateTime.Now.ToString ())); - SW. Flush (); + SW. Close (); A FS. Close (); at } - - Public voidInit () - { - //throw new NotImplementedException (); - } in - Public voidOnError (Exception ex) to { + //throw new NotImplementedException (); - } the } *}
Main program Portal Invoke service
1 namespaceMywindowstaskschedule2 {3 class Program4 {5 Static voidMain (string[] args)6 {7 varTest =NewTest ();8Test. Excute ();//Running the service9 }Ten } One}
Compile the program, the bin directory file is the service is about to run.
So the question is, how do I run?
Let me just attach a few more simple instructions.
Bin directory: Stores the console's compiled files, including configuration files.
Configs directory: Two profiles jobs.config and Nlog.config
The jobs.config configuration is as follows:
1 <?XML version= "1.0" encoding= "Utf-8"?>2 <JobsServerName= "My-service"DisplayName= "My Service"Description= "My service description">3 <Jobname= "My service One"ExePath= "${basedir}\bin\mywindowstaskschedule.exe"arguments= "1"cornexpress= "0/2 * * * *?"Expiresecond= "+" />4 </Jobs>
ServerName: The name that the service displays in Task Manager
DisplayName: The name that the service displays in the list of services
Job node: Specific configuration of individual tasks, name recommendation unique, exepath task path, cornexpress task execution frequency
In practical applications, we need to modify the jobs.config,nlog.config to be fixed.
Installation Services. bat
1 F:\maiaimei\WindowsService\MyWindowsTaskSchedule\Demo\Windows.TaskSchedule. exe install 2 Start 3 Pause
Uninstall the service. bat
1 F:\maiaimei\WindowsService\MyWindowsTaskSchedule\Demo\Windows.TaskSchedule. exe stop 2 F:\maiaimei\WindowsService\MyWindowsTaskSchedule\Demo\Windows.TaskSchedule. exe Uninstall 3 Pause
Above two batch commands, Windows.taskschedule. exe before a large string, please adjust as needed.
Other files can be copied directly.
When everything is ready (the demo directory above), we run "install service. Bat" as an administrator, as follows:
We can then check in the list of services and Task Manager
If you run the Uninstall service. BAT, the effect is as follows:
Source and demo Download: Https://github.com/maiaimei/WindowsTaskSchedule
Reference URL:
Open source a timed task frame under Windows, simple and rough to use.
C # uses the Timed task framework Windows.TaskSchedule.exe to install the Windows service that the console application creates