如何在特定系统时间触发窗口服务(例如:12:30 AM)

时间:2014-08-02 10:26:05

标签: c# windows-services

我已经编写了这个工作代码,每3秒钟启动一次窗口服务,但是无法像上午12:30那样实现特定的系统时间。这段代码工作正常,但我需要用特定的系统时间来实现。请指导我。

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 System.Data.SqlClient;
using System.Configuration;

namespace WindowsServiceForOnlineMockTest
{
    public partial class Service1 : ServiceBase
    {
        System.Timers.Timer sysTimer = new System.Timers.Timer();
        private int _interval = 3000;
        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            sysTimer.Interval = _interval;
            sysTimer.Enabled = true;
            sysTimer.Start();
            sysTimer.Elapsed += new System.Timers.ElapsedEventHandler(onElapsed);
        }

        protected override void OnStop()
        {
        }


        private void onElapsed(object sender, System.Timers.ElapsedEventArgs e)
        {

            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString);
            try
            {
                SqlCommand cmd = new SqlCommand("changeid", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                conn.Open();
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                EventLog.WriteEntry("Error", ex.Message, EventLogEntryType.Warning);
            }
        }
    }
}

0 个答案:

没有答案