如何使用c#[复制]每周调用一次方法

时间:2022-06-14 14:36:44

This question already has an answer here:

这个问题在这里已有答案:

Can any one say, 'How to call a particular method once every week in C# console application'

任何人都可以说,“如何在C#控制台应用程序中每周调用一次特定方法”

Thank you.

3 个解决方案

#1


If you just want to start the code from an external application, the Windows Task Scheduler is an option. It allows you to start an executable automatically.

如果您只想从外部应用程序启动代码,则可以选择Windows任务计划程序。它允许您自动启动可执行文件。

#2


A console app feels like a fragile way of doing this. I suggest you separate the scheduling of the task from the task itself. Windows has built in scheduling support in the form of the Task Scheduler (completely different from its namesake in .Net TPL).

控制台应用程序感觉像是一种脆弱的方式。我建议你将任务的调度与任务本身分开。 Windows以任务计划程序的形式内置了计划支持(完全不同于.Net TPL中的同名)。

So build your console application and schedule it to be run weekly. If it needs to maintain state during the week then implement it as a Windows service that can keep running.

因此,构建您的控制台应用程序并安排它每周运行。如果它需要在一周内维护状态,那么将其实现为可以继续运行的Windows服务。

There is a nice wrapper on the Task Scheduler API here: https://taskscheduler.codeplex.com/ so you could even configure the scheduled task from .Net.

Task Scheduler API上有一个很好的包装器:https://taskscheduler.codeplex.com/所以你甚至可以从.Net配置计划任务。

#3


You've got multiple options.

你有多种选择。

The one I use is adding the application to the task scheduler. There you can specify the interval in wchich it should be executed. The disadvantege of this method is that you cant have any other method executed in another interval unless you also add them to their own application.

我使用的是将应用程序添加到任务调度程序。在那里你可以指定它应该执行的间隔。这种方法的缺点是你不能在另一个间隔中执行任何其他方法,除非你也将它们添加到他们自己的应用程序中。

The second option is using System.Threading.Thread.Sleep If you want to use it, just do something like:

第二个选项是使用System.Threading.Thread.Sleep如果你想使用它,只需执行以下操作:

while(true)
{
//execute a method
System.Threading.Thread.Sleep(1000*60*60*24*7);
}

The disadvantage here is that you have to use threading if you want to execute other methods during the sleeptime.

这里的缺点是如果要在休眠时间执行其他方法,则必须使用线程。

#1


If you just want to start the code from an external application, the Windows Task Scheduler is an option. It allows you to start an executable automatically.

如果您只想从外部应用程序启动代码,则可以选择Windows任务计划程序。它允许您自动启动可执行文件。

#2


A console app feels like a fragile way of doing this. I suggest you separate the scheduling of the task from the task itself. Windows has built in scheduling support in the form of the Task Scheduler (completely different from its namesake in .Net TPL).

控制台应用程序感觉像是一种脆弱的方式。我建议你将任务的调度与任务本身分开。 Windows以任务计划程序的形式内置了计划支持(完全不同于.Net TPL中的同名)。

So build your console application and schedule it to be run weekly. If it needs to maintain state during the week then implement it as a Windows service that can keep running.

因此,构建您的控制台应用程序并安排它每周运行。如果它需要在一周内维护状态,那么将其实现为可以继续运行的Windows服务。

There is a nice wrapper on the Task Scheduler API here: https://taskscheduler.codeplex.com/ so you could even configure the scheduled task from .Net.

Task Scheduler API上有一个很好的包装器:https://taskscheduler.codeplex.com/所以你甚至可以从.Net配置计划任务。

#3


You've got multiple options.

你有多种选择。

The one I use is adding the application to the task scheduler. There you can specify the interval in wchich it should be executed. The disadvantege of this method is that you cant have any other method executed in another interval unless you also add them to their own application.

我使用的是将应用程序添加到任务调度程序。在那里你可以指定它应该执行的间隔。这种方法的缺点是你不能在另一个间隔中执行任何其他方法,除非你也将它们添加到他们自己的应用程序中。

The second option is using System.Threading.Thread.Sleep If you want to use it, just do something like:

第二个选项是使用System.Threading.Thread.Sleep如果你想使用它,只需执行以下操作:

while(true)
{
//execute a method
System.Threading.Thread.Sleep(1000*60*60*24*7);
}

The disadvantage here is that you have to use threading if you want to execute other methods during the sleeptime.

这里的缺点是如果要在休眠时间执行其他方法,则必须使用线程。