Add window service of c (scheduled task)

This article describes how to create a scheduled task using the window service

 

1. As shown in the figure, create a new project, windows Desktop - > windows Service

 

2. As shown in the figure, right click to add the installation program

 

 

3. Right click serviceInstaller1, and modify serviceName and Description

 

4. As shown in the figure, right-click serviceProcessInstaller and change Account to LocalSystem

 

5. Add code to Service1

 public partial class Service1 : ServiceBase
    {
        //Record to event log , address is C:\Windows\System32\winevt\Logs (Double click to view. The file name is MyNewLog)
        private static EventLog eventLog1;
        private int eventId = 1;

        public Service1()
        {
            InitializeComponent();

            eventLog1 = new System.Diagnostics.EventLog();
            if (!System.Diagnostics.EventLog.SourceExists("MySource"))
            {
                System.Diagnostics.EventLog.CreateEventSource(
                    "MySource", "MyNewLog");
            }
            eventLog1.Source = "MySource";
            eventLog1.Log = "MyNewLog";
        }

        /// <summary>
        /// Startup service
        /// </summary>
        /// <param name="args"></param>
        protected override void OnStart(string[] args)
        {
            eventLog1.WriteEntry("In OnStart.");
            log("In OnStart.");

            // Set up a timer that triggers every minute. set timer 
            Timer timer = new Timer();
            timer.Interval = 60000; // 60 seconds 60 Once per second
            timer.Elapsed += new ElapsedEventHandler(this.OnTimer);
            timer.Start();
        }

        /// <summary>
        /// Out of Service
        /// </summary>
        protected override void OnStop()
        {
            eventLog1.WriteEntry("In OnStop.");
            log("In OnStop.");
        }

        /// <summary>
        /// Continue service
        /// </summary>
        protected override void OnContinue()
        {
            eventLog1.WriteEntry("In OnContinue.");
            log("In OnContinue.");
        }

        /// <summary>
        /// Tasks to be executed at fixed time in timer
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        public void OnTimer(object sender, ElapsedEventArgs args)
        {
            // TODO: Insert monitoring activities here.
            eventLog1.WriteEntry("Monitoring the System", EventLogEntryType.Information, eventId++);
            log("the timer");
        }



        /// <summary>
        /// Log to the specified path: D:\log.txt
        /// </summary>
        /// <param name="message"></param>
        private static void log(string message)
        {
            using (FileStream stream = new FileStream("D:\\log.txt", FileMode.Append))
                using(StreamWriter writer=new StreamWriter(stream))
            {
                writer.WriteLine($"{DateTime.Now}:{message}");
            }
        }

    }

 

The structure is shown in the figure:

 

6. Right click to generate solution

 

7. Open the project, copy the bin directory to the windows service Hello folder of the new windowServiceTest folder under the C disk, and copy the InstallUtil.exe under C:\Windows\Microsoft.NET\Framework\v4.0.30319 to the windowServiceTest folder, as shown in the figure below

 

 

 

8. Installation services

Open cmd (as administrator) and enter the windowServiceTest folder

Installation services:

InstallUtil.exe  C:\windowServiceTest\windowService_Hello\bin\Debug\WindowService_HelloWorld.exe

Design sketch:

 

9. Open the service manager, start the MyService service, wait for a few minutes, and then uninstall the service

Uninstall service:

InstallUtil.exe  -u C:\windowServiceTest\windowService_Hello\bin\Debug\WindowService_HelloWorld.exe

 

 

10. Check whether it has effect

Find the log.txt file on disk D, and open it as follows

 

The event log is shown in the figure

 

 

It can be seen that the scheduled task on the window service has taken effect

 

Reference website

https://docs.microsoft.com/en-us/dotnet/framework/windows-services/walkthrough-creating-a-windows-service-application-in-the-component-designer

Keywords: ASP.NET Windows

Added by cutups on Mon, 11 Nov 2019 17:04:25 +0200