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

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

问题描述

我搜索了 SO 并找到了有关 Quartz.net 的答案.但这对我的项目来说似乎太大了.我想要一个等效的解决方案,但更简单并且(充其量)代码中(不需要外部库).如何每天在特定时间调用方法?

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#?

我已经做到了.我在 void Main() 中添加了一个参数,并创建了一个 bat(由 Windows Task Scheduler 调度)以使用该参数运行程序.程序运行,完成工作,然后退出.如果作业失败,它可以写入日志并发送电子邮件.这种方法很符合我的要求:)

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
    • 这就是你所需要的!

      更新:如果您想在您的应用中执行此操作,您有多种选择:

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

      • Windows 窗体 应用中,您可以访问 Application.Idle 事件并检查您是否已经到了当天调用您的方法的时间.此方法仅在您的应用程序不忙于处理其他事情时调用.快速检查一下是否已达到您的目标时间不应给您的应用带来太大压力,我认为...
      • 在 ASP.NET Web 应用程序中,有一些方法可以模拟"发送预定事件 - 请查看此 CodeProject 文章
      • 当然,您也可以在任何 .NET 应用程序中简单地推出自己的" - 查看此 CodeProject 文章示例实现
      • 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天全站免登陆