简体   繁体   English

定时器每周抛出一次方法 - C#,Asp.NET

[英]Timer to throw Method Once a Week - C#, Asp.NET

I'm fairly new to C# and coding in general. 我对C#和编码很新。 I'm looking for an example/open source Timer Function that will be used to throw a basic method (ex. Email Notifications) once a week at a certain time. 我正在寻找一个示例/开源定时器函数,用于在特定时间每周一次抛出基本方法(例如电子邮件通知)。 After doing some research I've found that using a Timer instead of a Windows Service would be wise as it will have a very small workload. 在做了一些研究后,我发现使用Timer而不是Windows服务是明智的,因为它的工作量非常小。

I've found a couple timer API's on CodePlex and Code Project but am having trouble working my way through the Examples due to my minimal knowledge of C#. 我在CodePlex和Code Project上找到了几个计时器API,但由于我对C#的了解很少,我在使用这些示例时遇到了麻烦。

Does anyone know of a Simple timer that I could use that is Beginner friendly and that also has a walk through? 有没有人知道我可以使用的简单计时器是初学者友好的,还有一个漫步? Or is there a simpler way of doing this using System.Timers? 或者使用System.Timers有一种更简单的方法吗?

My end goal is to have a timer that will fire a Method at 8:00AM every Monday. 我的最终目标是拥有一个计时器,它将在每个星期一的上午8:00触发一个方法。

EDIT: I believe I should have been more detailed in my initial Post. 编辑:我相信我应该在我的初始帖子中更详细。 The reason I chose not to use the Windows Task Scheduler is because the Method I am invoking is more complex than just invoking Windows Task. 我选择不使用Windows任务计划程序的原因是因为我调用的方法比调用Windows任务更复杂。

When the timer hits the scheduled time it will fire a Method which queries the DB for Items requiring service within a certain time-frame. 当计时器达到预定时间时,它将触发一个方法,该方法在数据库中查询在特定时间范围内需要服务的项目。 This will then loop through the items adding them to an email which will be sent to that Warehouse's Admin. 然后,这将循环显示将其添加到电子邮件中的项目,该电子邮件将发送到该仓库的管理员。

If I'm not mistaken, that cannot be done through Windows Task Scheduler? 如果我没有弄错,那么无法通过Windows任务计划程序完成?

Using a timer for this makes no sense. 使用计时器是没有意义的。 Your goal is to have a function fire at a specific time. 您的目标是在特定时间触发功能。 What if your server reboots? 如果您的服务器重新启动怎么办? Is your timer going to calculate the exact time between the reboot time and Monday at 8:00 am? 您的计时器是否计算重启时间与周一上午8:00之间的确切时间?

IMO, use a console application and the Windows Task Scheduler. IMO,使用控制台应用程序和Windows任务计划程序。

Response to edit : Of course you can use Windows Task Scheduler for this. 响应编辑 :当然,您可以使用Windows任务计划程序。 The only function of the scheduler is to execute a task. 调度程序的唯一功能是执行任务。 There are no rules as to the complexity of the task it executes. 它执行的任务的复杂性没有规则。 Plus, querying a DB and sending an email isn't exactly an overly complex task. 此外,查询数据库和发送电子邮件并不是一项过于复杂的任务。 :) :)

I use Quartz.NET for cron jobs. 我使用Quartz.NET进行cron作业。 This works on a hosted platform as well, where you have no access to windows services. 这也适用于托管平台,您无法访问Windows服务。

What you are looking for would be trivial to achieve. 您正在寻找的将是微不足道的。 Look at the first example from their tutorial . 查看他们教程中的第一个示例。

http://quartznet.sourceforge.net/tutorial/lesson_1.html http://quartznet.sourceforge.net/tutorial/lesson_1.html

我不同意您的研究,并将其放入由Windows Scheduler运行的控制台应用程序中。

Since you you are fairly new to coding, the easiest thing to do would be to create an application (console based c# application would work the best) and use 既然您对编码很陌生,最简单的方法就是创建一个应用程序(基于控制台的c#应用程序将是最好的)并使用

Task Scheduler 任务计划程序

that's built into windows to launch the app 8:00am every monday. 每个星期一早上8点都会内置到windows中以启动应用程序。

A cron job would be a much better option. 一个cron工作将是一个更好的选择。 What would happen if your website is down at 8 AM Monday morning? 如果您的网站在周一早上8点停电,会发生什么? With cron, as long as the server is running it can execute your request. 使用cron,只要服务器正在运行,它就可以执行您的请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM