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

问题描述

我已经创建了Windows服务,并且希望该服务计划在每天的6:00 AM运行。
以下是我编写的代码:-

I had created a windows service and i want that the service will Schedule to run daily at 6:00 Am.Below is the code which i had written:-

public Service1()
{
    InitializeComponent();
}

protected override void OnStart(string[] args)
{
    try
    {
        ExtractDataFromSharePoint();
    }
    catch (Exception ex)
    {
        //Displays and Logs Message
        _loggerDetails.LogMessage = ex.ToString();
        _writeLog.LogDetails(_loggerDetails.LogLevel_Error, _loggerDetails.LogMessage);
    }
}

在上面的代码中,您可以在 OnStart 服务方法我正在调用函数 ExtractDataFromSharePoint()。我将如何安排它每天早上6:00运行。

In the above code you can see that in OnStart Method of service i am calling a Function ExtractDataFromSharePoint(). How i will schedule this to run daily morning at 6:00 AM.

推荐答案

在这里,您有2种执行应用程序的方式每天早上6点运行。

Here, you have 2 ways to execute your application to run at 6 AM daily.

1)创建一个控制台应用程序,并通过Windows Scheduler在早上6点执行。

1) Create a console application and through windows scheduler execute on 6 AM.

2)在Windows服务中创建一个计时器(System.Timers.Timer),该计时器在每个定义的间隔和功能中执行,您必须检查系统时间= 6 AM,然后执行您的代码

2) Create a timer (System.Timers.Timer) in your windows service which executes on every defined interval and in your function, you have to check if the system time = 6 AM then execute your code

ServiceTimer = new System.Timers.Timer();
ServiceTimer.Enabled = true;
ServiceTimer.Interval = 60000 * Interval;
ServiceTimer.Elapsed += new System.Timers.ElapsedEventHandler(your function);

注意:在函数中,您必须编写代码以仅在上午6点执行方法时间

Note: In your function you have to write the code to execute your method on 6 AM only not every time

这篇关于Windows服务计划每天每天凌晨6:00运行一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 11:06