Vici WinService is a lightweight class library for creating, deleting services under the Windows platform using C #, and you can create, delete, and run multiple-threaded asynchronous services with just a few lines of code, directly on the code
/****************************************************************** * Created by: HTL * Created: 2015-5-12 14:09:39 * Description: Use Vici WinService component created by Windows Service * Email:[email protected] *************************************************************** ****/usingSystem;usingSystem.Text;usingNLog;//referencing the log componentusingVici.winservice;//Referencing ComponentsusingSystem.ServiceProcess;//Referencing system ComponentsnamespaceHTL. testservice{#regionLog Operation class/// <summary> ///Write Log/// </summary> classLog { Public StaticLogger _log =Logmanager.getcurrentclasslogger (); Public StaticLogger GetLog {Get{return_log;} } } #endregion #regionCustom Service Classes/// <summary> ///Custom Service Classes/// </summary> classTestservice:service { PublicTestservice ():Base(NewServiceInfo ("Test_server"))//the name and display name of the service{Servicetasks.add (NewTestservicetask ()); } } #endregion #regionCustomizing task classes in Services/// <summary> ///looping through tasks in a service/// </summary> classTestservicetask:cyclicservicetask {/// <summary> ///30 seconds to execute one task asynchronously/// </summary> PublicTestservicetask ():Base(Timespan.fromseconds ( -),false) { } Public StaticNlog.logger Logger =NLog.LogManager.GetCurrentClassLogger (); protected Override voidRunTask () {Log.GetLog.Info ("Custom Service task is working ....."); } } #endregion Static classProgram {Static voidMain (params string[] Parameters) {Log.GetLog.Info ("start running the program"); Testservice Service=NewTestservice (); //Run automaticallyService. Serviceinfo.servicestartmode =servicestartmode.automatic; if(Parameters. Length >0) { stringoption = parameters[0]. ToLower (); Switch(option) { Case "/console": {service. Runconsole (); Log.GetLog.Info ("running the console program"); } return; Case "/install": {service. Install (); Log.GetLog.Info ("Install services, run automatically"); } return; Case "/uninstall": {service. UnInstall (); Log.GetLog.Info ("Start Uninstall Service"); } return; }} service. Run (); } }}
How does it work? 1. Under cmd command line switch to exe build directory 2. Run the exe file, you need to add the running parameters (see the code above),closing the following command line after running does not affect the execution of the service3. Install/Uninstall the Service 4. Start the "Test_server" service 5. Review the task log for service execution (every 30 seconds, see code above)Reference:Vici WinServiceBlog Park: Lightweight Windows Services Create componentsOther References: Blog Park: Create a Windows Service (Windows Services) n ways to summarize blog Park: C # Verify that the Windows service exists and starts the blog park: Creating Windows Services with Topshelf How to manually add and remove Windows services
From for notes (Wiz)
C # uses Vici WinService components to create Windows services