Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何设置Windows服务的onStart()方法,以便在安装后在上午12点执行_C#_Windows Services - Fatal编程技术网

C# 如何设置Windows服务的onStart()方法,以便在安装后在上午12点执行

C# 如何设置Windows服务的onStart()方法,以便在安装后在上午12点执行,c#,windows-services,C#,Windows Services,如何设置Windows服务的onStart()方法,以便在安装Windows服务后,首先在上午12点执行,时间间隔可以正常工作,并且服务在上述时间间隔后执行,但不会在给定时间启动 public static System.Timers.Timer Timer; Double _timeinterval = 300 * 1000;// 6 mins protected override void OnStart(string[

如何设置Windows服务的onStart()方法,以便在安装Windows服务后,首先在上午12点执行,时间间隔可以正常工作,并且服务在上述时间间隔后执行,但不会在给定时间启动

            public static System.Timers.Timer Timer;
            Double _timeinterval = 300 * 1000;// 6 mins
            protected override void OnStart(string[] args)
            {
            Timer = new System.Timers.Timer();

            Timer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
            Timer.Interval = _timeinterval;
            Timer.Enabled = true;
            //method call to do operation
           }
            private void OnTimedEvent(object source, ElapsedEventArgs e)
              {
                //method call to do operation
              }
protected override void OnStart(字符串[]args)
{
aTimer=新的System.Timers.Timer();
字符串starttime=“01.25”;
//开始时间为01.25表示凌晨01:15
双分钟=转换为双分钟(开始时间);
DateTime t=DateTime.Now.Date.AddHours(分钟);
TimeSpan ts=新的TimeSpan();
//ts=t-System.DateTime.Now;
ts=t.AddDays(1)-System.DateTime.Now;
如果(总毫秒数<0)
{
ts=t.AddDays(1)-System.DateTime.Now;
//ts=t-System.DateTime.Now;
}
_时间间隔=ts.total毫秒;
//\u时间间隔现在设置为1:15 am(从现在到1:15 am的时间)
aTimer.Appead+=新的ElapsedEventHandler(OnTimedEvent);
aTimer.Interval=\u时间间隔;
aTimer.Enabled=true;
}
私有静态void OnTimedEvent(对象源,ElapsedEventArgs e)
{
//执行的操作
aTimer.Interval=86400000;//现在Interval设置为24小时
}

据我所知,您希望在凌晨12点准时开始操作,但由于您指定的时间间隔,操作会推迟几分钟。对的
  protected override void OnStart(string[] args)
    {

        aTimer = new System.Timers.Timer();

        string starttime = "01.25";
        //start time is 01.25 means 01:15 AM

        double mins = Convert.ToDouble(starttime);
        DateTime t = DateTime.Now.Date.AddHours(mins);
        TimeSpan ts = new TimeSpan();
        // ts = t - System.DateTime.Now;
        ts = t.AddDays(1) - System.DateTime.Now;
        if (ts.TotalMilliseconds < 0)
        {
            ts = t.AddDays(1) - System.DateTime.Now;
            // ts = t - System.DateTime.Now;
        }
        _timeinterval = ts.TotalMilliseconds;
       // _timeinterval now set to 1:15 am (time from now to 1:15AM)
        aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
        aTimer.Interval = _timeinterval;
        aTimer.Enabled = true;
       }
        private static void OnTimedEvent(object source, ElapsedEventArgs e)
         {

           // operation to perform
           aTimer.Interval = 86400000; // now interval sets to 24 hrs

    }