.net 定時執行 windows 服務

來源:互聯網
上載者:User

標籤:

1.建立項目 --》 Windows 服務 2.Service1.cs代碼

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  
{  
    public partial class Service : ServiceBase  
    {  
        public Service()  
        {  
            InitializeComponent();  
        }  
        protected override void OnStart(string[] args)  
        {  
            EventLog.WriteEntry("我的服務啟動");//在系統事件查看器裡的應用程式事件裡來源的描述  
            writestr("服務啟動");//自訂文本日誌  
            System.Timers.Timer t = new System.Timers.Timer();  
            t.Interval = 1000;  
            t.Elapsed += new System.Timers.ElapsedEventHandler(ChkSrv);//到達時間的時候執行事件;   
            t.AutoReset = true;//設定是執行一次(false)還是一直執行(true);   
            t.Enabled = true;//是否執行System.Timers.Timer.Elapsed事件;   
        }  
        /// <summary>  
        /// 定時檢查,並執行方法  
        /// </summary>  
        /// <param name="source"></param>  
        /// <param name="e"></param>  
        public void ChkSrv(object source, System.Timers.ElapsedEventArgs e)  
        {  
            int intHour = e.SignalTime.Hour;  
            int intMinute = e.SignalTime.Minute;  
            int intSecond = e.SignalTime.Second;  
             
            if (intHour == 13 && intMinute == 30 && intSecond == 00) ///定時設定,判斷分時秒  
            {  
                try  
                {  
                    System.Timers.Timer tt = (System.Timers.Timer)source;  
                    tt.Enabled = false;  
                    SetInnPoint();  
                    tt.Enabled = true;  
                }  
                catch (Exception err)  
                {  
                    writestr(err.Message);  
                }  
            }  
        }  
        //我的方法  
        public void SetInnPoint()  
        {  
            try  
            {  
                writestr("服務運行");  
                //這裡執行你的東西  
                Thread.Sleep(10000);  
            }  
            catch (Exception err)  
            {  
                writestr(err.Message);  
            }  
        }  
        ///在指定時間過後執行指定的運算式  
        ///  
        ///事件之間經過的時間(以毫秒為單位)  
        ///要執行的運算式  
        public static void SetTimeout(double interval, Action action)  
        {  
            System.Timers.Timer timer = new System.Timers.Timer(interval);  
            timer.Elapsed += delegate(object sender, System.Timers.ElapsedEventArgs e)  
            {  
                timer.Enabled = false;  
                action();  
            };  
            timer.Enabled = true;  
        }  
  
        public void writestr(string readme)  
        {  
            //debug==================================================  
            //StreamWriter dout = new StreamWriter(@"c:/" + System.DateTime.Now.ToString("yyyMMddHHmmss") + ".txt");  
            StreamWriter dout = new StreamWriter(@"c:/" + "WServ_InnPointLog.txt", true);  
            dout.Write("/r/n事件:" + readme + "/r/n操作時間:" + System.DateTime.Now.ToString("yyy-MM-dd HH:mm:ss"));  
            //debug==================================================  
            dout.Close();  
        }  
        protected override void OnStop()  
        {  
            writestr("服務停止");  
            EventLog.WriteEntry("我的服務停止");  
        }  
    }  
}  

 

3.在Service1.cs設計頁面右鍵添加安裝程式

4.ProjectInstaller.cs設計頁面中

   serviceInstaller1屬性中設定:

    Description(系統服務的描述)

    DisplayName (系統服務中顯示的名稱)

    ServiceName(系統事件查看器裡的應用程式事件中來源名稱)

   serviceProcessInstaller1屬性設定:

      Account 下拉設定成 LocalSystem

5.在.net Framework的安裝目錄可以找到InstallUtil.exe,可以複製出來放在你的服務產生的exe一起

6.安裝服務setup.bat批次檔內容:InstallUtil Service1.exe ,安裝好後可以在系統服務中找到你設定的服務名稱然後可以啟動這個服務

7.卸載服務UnInstall.bat批次檔內容:InstallUtil -u Service1.exe

 

轉自:http://blog.csdn.net/windxxf/article/details/6014538

.net 定時執行 windows 服務

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.