如何每日调用一个方法,在特定的时间,在C#? [英] How to call a method daily, at specific time, in C#?

查看:524
本文介绍了如何每日调用一个方法,在特定的时间,在C#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索的SO,发现约Quartz.net答案。但它似乎是太大了,我的项目。我想一个等效的解决方案,但更简单和(最好)IN- code(无需外部库)。我怎样才能每天调用方法,在特定的时间?

I've searched on SO and found answers about Quartz.net. But it seems to be too big for my project. I want an equivalent solution, but simpler and (at best) in-code (no external library required). How can I call a method daily, at a specific time?

我需要补充一些有关此信息:

I need to add some information about this:


  • 要做到这一点最简单的(丑陋的)的方式,是检查的时间每一秒/分钟,调用该方法,在合适的时间

我想这样做更有效的方式,无需不断检查的时间,我对这份工作是否做了无法控制。如果方法失败(因为任何问题),程序应该知道写入日志/发送电子邮件。这就是为什么我需要调用一个方法,而不是计划作业。

I want a more-effective way to do this, no need to check the time constantly, and I have control about whether the job is done a not. If the method fails (because of any problems), the program should know to write to log/send a email. That's why I need to call a method, not schedule a job.

我发现这个解决方案在Java中在固定的时间调用方法在Java中。有没有在C#中以类似的方式?

I found this solution Call a method at fixed time in Java in Java. Is there a similar way in C#?

编辑:我已经做到了这一点。我添加了一个参数为无效的主要(),并创建了一个蝙蝠(通过Windows任务计划安排)来运行这个参数的程序。程序运行时,做这项工作,然后退出。如果作业失败,它能够写日志和发送电子邮件。这种方法适合我的要求还有:)

I've done this. I added a parameter into void Main(), and created a bat (scheduled by Windows Task Scheduler) to run the program with this parameter. The program runs, does the job, and then exits. If a job fails, it's capable of writing log and sending email. This approach fits my requirements well :)

推荐答案


  • 创建一个控制台应用程序已经做了你在找什么

  • 使用Windows的计划任务功能,必须在执行的控制台应用程序你需要它的时候跑

  • Create a console app that does what you're looking for
  • Use the Windows "Scheduled Tasks" functionality to have that console app executed at the time you need it to run
  • 这的确是所有你需要的!

    That's really all you need!

    更新:如果您想这样做,你的应用程序里面,你有几种选择:

    Update: if you want to do this inside your app, you have several options:


    • 在一个 Windows窗体应用,你可以挖掘到<一个href=\"http://msdn.microsoft.com/en-us/library/system.windows.forms.application.idle.aspx\"><$c$c>Application.Idle事件并查看是否已经达到了一天时间打电话给你的方法。当你的应用是不是忙于其他的东西,这种方法只调用。快速检查,看看你的目标时间已经达到了不应该把太多的压力对你的应用程序,我想......

    • 在一个ASP.NET Web应用程序,也有方法来模拟发送安排的活动 - 看看这个的 $ C $的CProject文章

    • ,当然,你也可以只是简单地在任何.NET应用程序滚你自己 - 看看这个的有关示例实现$ C $的CProject文章

    • in a Windows Forms app, you could tap into the Application.Idle event and check to see whether you've reached the time in the day to call your method. This method is only called when your app isn't busy with other stuff. A quick check to see if your target time has been reached shouldn't put too much stress on your app, I think...
    • in a ASP.NET web app, there are methods to "simulate" sending out scheduled events - check out this CodeProject article
    • and of course, you can also just simply "roll your own" in any .NET app - check out this CodeProject article for a sample implementation

    更新#2 :如果您要检查每60分钟,你可以创建一个定时器,唤醒每60分钟,如果时间到了,它会调用该方法

    Update #2: if you want to check every 60 minutes, you could create a timer that wakes up every 60 minutes and if the time is up, it calls the method.

    事情是这样的:

    using System.Timers;
    
    const double interval60Minutes = 60 * 60 * 1000; // milliseconds to one hour
    
    Timer checkForTime = new Timer(interval60Minutes);
    checkForTime.Elapsed += new ElapsedEventHandler(checkForTime_Elapsed);
    checkForTime.Enabled = true;
    

    ,然后在你的事件处理程序:

    and then in your event handler:

    void checkForTime_Elapsed(object sender, ElapsedEventArgs e)
    {
        if (timeIsReady())
        {
           SendEmail();
        }
    }
    

    这篇关于如何每日调用一个方法,在特定的时间,在C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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