C# 每 x 分钟调用一次方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13019433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:22:36  来源:igfitidea点击:

Calling a method every x minutes

c#

提问by user1765862

I want to call some method on every 5 minutes. How can I do this?

我想每 5 分钟调用一次方法。我怎样才能做到这一点?

public class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("*** calling MyMethod *** ");
        Console.ReadLine();
    }

    private MyMethod()
    {
        Console.WriteLine("*** Method is executed at {0} ***", DateTime.Now);
        Console.ReadLine();
    }
}

采纳答案by asawyer

var startTimeSpan = TimeSpan.Zero;
var periodTimeSpan = TimeSpan.FromMinutes(5);

var timer = new System.Threading.Timer((e) =>
{
    MyMethod();   
}, null, startTimeSpan, periodTimeSpan);

回答by Chuck Conway

Use a Timer. Timer documentation.

使用一个Timer. 定时器文档

回答by Dennis Traub

while (true)
{
    Thread.Sleep(60 * 5 * 1000);
    Console.WriteLine("*** calling MyMethod *** ");
    MyMethod();
}

回答by André C. Andersen

I based this on @asawyer's answer. He doesn't seem to get a compile error, but some of us do. Here is a version which the C# compiler in Visual Studio 2010 will accept.

我基于@asawyer 的回答。他似乎没有收到编译错误,但我们中的一些人却收到了。这是 Visual Studio 2010 中的 C# 编译器将接受的版本。

var timer = new System.Threading.Timer(
    e => MyMethod(),  
    null, 
    TimeSpan.Zero, 
    TimeSpan.FromMinutes(5));

回答by PJ3

Example of using a Timer:

使用 a 的示例Timer

using System.Timers;

static void Main(string[] args)()
{
    Timer t = new Timer(TimeSpan.FromMinutes(5).TotalMilliseconds); // Set the time (5 mins in this case)
    t.AutoReset = true;
    t.Elapsed += new System.Timers.ElapsedEventHandler(your_method);
    t.Start();
}

// This method is called every 5 mins
private void your_method(object sender, ElapsedEventArgs e)
{
    Console.WriteLine("..."); 
}

回答by Maiko Kingma

Start a timer in the constructor of your class. The interval is in milliseconds so 5*60 seconds = 300 seconds = 300000 milliseconds.

在类的构造函数中启动计时器。间隔以毫秒为单位,所以 5*60 秒 = 300 秒 = 300000 毫秒。

static void Main(string[] args)
{
    System.Timers.Timer timer = new System.Timers.Timer();
    timer.Interval = 300000;
    timer.Elapsed += timer_Elapsed;
    timer.Start();
}

Then call GetData()in the timer_Elapsedevent like this:

然后调用GetData()timer_Elapsed这样的事件:

static void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
    //YourCode
}

回答by Ahmed Abuelnour

I've uploaded a Nuget Package that can make it so simple, you can have it from here ActionScheduler

我上传了一个 Nuget 包,它可以让它变得如此简单,你可以从这里获取它ActionScheduler

It supports .NET Standard 2.0

它支持 .NET Standard 2.0

And here how to start using it

在这里如何开始使用它

using ActionScheduler;

var jobScheduler = new JobScheduler(TimeSpan.FromMinutes(8), new Action(() => {
  //What you want to execute
}));

jobScheduler.Start(); // To Start up the Scheduler

jobScheduler.Stop(); // To Stop Scheduler from Running.