Windows服务每运行一小时 [英] Windows service run for every one hour

查看:137
本文介绍了Windows服务每运行一小时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Windows服务,应该每隔一小时运行一次。但问题是Ex:它从早上6点开始,到凌晨6点30分结束。然后在一小时后再次开始运行于上午7点3分。但我希望它能在上午7点再次运行。

任何人的帮助将不胜感激。

代码我写的是下面



什么我试过:



I have a windows service which should run for every one hour. But the issue is Ex: it starts at 6.00 A.m and ends at 6.03 A.M. Then again after one hour its starting running at 7.03 A.M. But I want it to run again at 7.00 A.M.
Anyone's help will be appreciated.
The code What I have written is below

What I have tried:

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.Threading;
using System.Threading.Tasks;

namespace Ashworth.WindowService.GradeDataFeed
{
    public partial class GradeDataFeedService : ServiceBase
    {
            private readonly System.Timers.Timer _timer;
            GradeDataFeedManager gradeDataFeedManager;
            //Thread managerThread = null;

            public GradeDataFeedService()
            {
                InitializeComponent();

                _timer = new System.Timers.Timer();
                _timer.Elapsed += timer_Elapsed;
            }

            protected override void OnStart(string[] args)
            {
                _timer.Interval = 60 * 1000;
                _timer.Enabled = true;
                _timer.Start();
            }

            private void timer_Elapsed(object sender, EventArgs e)
            {
                // Only create / start the manager if has been done so already
                // It should only all in once, since the manager object will not
                // be null and since the timer will be disabled after first run
                if (gradeDataFeedManager == null)
                {
                gradeDataFeedManager = new GradeDataFeedManager();

                // Create thread for manager
                //managerThread = new Thread(shipmentTrackingNumberProcessorManager.Start);

                // Start the thread
                gradeDataFeedManager.Start();

                    // Stop timer, it only needs to run once to kick off the manager
                    _timer.Enabled = false;
                }
            }

            protected override void OnStop()
            {
                if (gradeDataFeedManager != null)
                {
                gradeDataFeedManager.Stop();
                }
            }
        }
    }

推荐答案

首先,你要'需要服务。你需要一个从预定任务运行的简单控制台应用程序



否则你必须每隔一秒检查当前时间,比如说当前时间是否开启小时
First off, you don't need a service. You need a simple console app that runs from a scheduled task

Otherwise you have to check the current time every, say 1 second, to see if the current time is on the hour


这篇关于Windows服务每运行一小时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆