. NET timed execution of Windows services

Source: Internet
Author: User

1. New Project--"Windows service 2.service1.cs Code

Using System;  
Using System.Collections.Generic;  
Using System.ComponentModel;  
Using System.Data;  
Using System.Diagnostics;  
Using System.ServiceProcess;  
Using System.IO;  
Using System.Text;  
Using System.Timers;  
Using System.Data.SqlClient;  
Using System.Threading;  
Namespace Innpoint  
{  
    Class Service:servicebase  
    {  
        Public Service ()  
        {  
            InitializeComponent ();  
        }  
        void OnStart (string[] args)  
        {  
            EventLog.WriteEntry ("My service Starts");  Description  of the source in the application event in the System Event Viewer 
            WRITESTR ("service start");  Custom text log   
            New System.Timers.Timer ();  
            T.interval = 1000;  
            New System.Timers.ElapsedEventHandler (CHKSRV); //arrival time execution of the event;   
            True //Set whether to execute (false) or always execute (true);   
            True //Whether to perform System.Timers.Timer.Elapsed   events;
        }  
        <summary>  
        Timed check, and execution method  
        </summary>  
        <param name= "source" ></param>  
        <param name= "E" ></param>  
        void Chksrv (object source, System.Timers.ElapsedEventArgs e)  
        {  
            int inthour = E.signaltime.hour;  
            int intminute = E.signaltime.minute;  
            int intsecond = E.signaltime.second;  
             
            Timing setting to determine time-sharing seconds  
            {  
                Try  
                {  
                    System.Timers.Timer TT = (System.Timers.Timer) source;  
                    False  
                    Setinnpoint ();  
                    True  
                }  
                catch (Exception err)  
                {  
                    Writestr (Err. Message);  
                }  
            }  
        }  
        My way.  
        void Setinnpoint ()  
        {  
            Try  
            {  
                WRITESTR ("service Run");  
                Here, execute your stuff.  
                Thread.Sleep (10000);  
            }  
            catch (Exception err)  
            {  
                Writestr (Err. Message);  
            }  
        }  
        Executes the specified expression after the specified time  
        ///  
        Elapsed time (in milliseconds) between events  
        The expression to execute  
        void SetTimeout (double interval, action action)  
        {  
            New System.Timers.Timer (interval);  
            Delegate (object sender, System.Timers.ElapsedEventArgs e)  
            {  
                False  
                Action ();  
            };  
            True  
        }  
  
        void Writestr (string readme)  
        {  
            debug==================================================  
            StreamWriter dout = new StreamWriter (@ "c:/" + System.DateTime.Now.ToString ("Yyymmddhhmmss") + ". txt");  
            New StreamWriter (true);  
            Dout. Write ("/r/n operation time:" + System.DateTime.Now.ToString ("Yyy-mm-dd HH:mm:ss"));   
            debug==================================================  
            Dout. Close ();  
        }  
        void OnStop ()  
        {  
            WRITESTR ("service Stop");  
            EventLog.WriteEntry ("My service is stopped");  
        }  
    }  
}  

3. Right-click on the Service1.cs design page to add the installer

4.projectinstaller.cs the design page

The ServiceInstaller1 property is set in:

Description (Description of system service)

DisplayName (name displayed in system service)

ServiceName (source name in the application event in the System Event Viewer)

ServiceProcessInstaller1 Property settings:

Account drop-down set to LocalSystem

5. The. NET Framework installation directory can be found in InstallUtil.exe, which can be copied out and placed in your service generated EXE together

6. Installation Service Setup.bat batch file contents: InstallUtil Service1.exe, after installation, you can find the service name you set in the system services and then you can start the service

7. Uninstall Service Uninstall.bat batch file contents: Installutil-u Service1.exe

Transferred from: http://blog.csdn.net/windxxf/article/details/6014538

. NET timed execution of Windows services

Related Article

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.