添加服務,用於定期執行某個程式或者應用程式(windows service)

來源:互聯網
上載者:User

標籤:blog   os   資料   io   art   re   

需要配合window的安裝程式一起寫






using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Diagnostics;using System.Linq;using System.ServiceProcess;using System.Text;using Inspur.DataExchange.Logging;using System.Configuration;using Inspur.Finix.ExceptionManagement;namespace WenZhouZJ.Exchange.FromNW{ public partial class Service1 : ServiceBase { private System.Timers.Timer timer2; public Service1() { this.timer2 = new System.Timers.Timer(); ((System.ComponentModel.ISupportInitialize)(this.timer2)).BeginInit(); this.timer2.Enabled = true; this.timer2.Interval = 1 * 1000; this.timer2.Elapsed += new System.Timers.ElapsedEventHandler(this.timer2_Elapsed); ((System.ComponentModel.ISupportInitialize)(this.timer2)).EndInit(); this.ServiceName = "溫州質監與南威資料交換_收件資訊擷取服務"; } protected override void OnStart(string[] args) { //this.timer1.Enabled = false; } protected override void OnStop() { this.timer2.Enabled = false; } private void timer2_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { //如果到了設定的已耗用時間 string runtime = ConfigurationSettings.AppSettings["Runtime"]; if (string.IsNullOrEmpty(runtime) || runtime.Equals(DateTime.Now.ToString("HH:mm"))) { this.timer2.Enabled = false; try { //DataExchange.DoExchange(); 定期執行的事件 } catch (Exception ex) { ExceptionManager.Handle(ex); } this.timer2.Interval = Convert.ToDouble(System.Configuration.ConfigurationSettings.AppSettings["Interval"]) * 60 * 1000; this.timer2.Enabled = true; } } }}


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.