我需要每5秒执行一次方法。如何使用计时器概念? [英] I need to execute a method for every 5 seconds. How to do it with the timer concept?
问题描述
我需要每5秒执行一次方法。如何使用Timer概念?
我需要知道计时器是否运行并且每5秒就会触及一次方法。所以我尝试了下面的代码。
int autoPollingTime = Convert.ToInt32(configparams [AutoPollQueues]);
System.Timers。 Timer timer = new System.Timers.Timer();
timer.Interval = autoPollingTime; //以毫秒为单位的时间
if(autoPollingTime!= 0)
{
timer.Start();
if(timer.Interval == autoPollingTime)
{
Check();
Console.WriteLine(Timer completed);
Thread.Sleep(5000);
}
其他
{
返回;
}
}
在调试代码时,我应该每5秒钟收到一次控制台消息。但我没有得到它。我不知道是什么问题。
亲切帮助。
我的尝试:
谷歌搜索但没有得到问题的确切修复
I need to execute a method for every 5 seconds. How to do it with the Timer concept?
I need to know whether the timer runs and hits the method for every 5 seconds. So I tried the below code.
int autoPollingTime = Convert.ToInt32(configparams["AutoPollQueues"]);
System.Timers.Timer timer = new System.Timers.Timer();
timer.Interval = autoPollingTime; // time in Milliseconds
if (autoPollingTime != 0)
{
timer.Start();
if (timer.Interval == autoPollingTime)
{
Check();
Console.WriteLine("Timer completed");
Thread.Sleep(5000);
}
else
{
return;
}
}
While debugging the code, I should have got the console message for every 5 seconds. But I am not getting it. I do not know what is the problem.
Kindly Help.
What I have tried:
Googled but not getting the exact fix for the issue
推荐答案
不要使用thread.Sleep - 这可以防止你的UI线程显示任何东西。
Interval不会随着计时器的运行而改变 - 它是连续计时器激活之间的滴答数。
您需要做的是处理Timer.Elapsed事件以找出计时器何时被触发:
Don't use thread.Sleep - that prevents your UI thread from displaying anything.
And Interval doesn't change as the timer is running - it's the number of ticks between successive timer "activations".
What you need to do is handle the Timer.Elapsed event to find out when the timer has "fired":
void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
Console.WriteLine("Timer elapsed");
}
private void MyButton_Click(object sender, EventArgs ew)
{
System.Timers.Timer timer = new System.Timers.Timer();
timer.Interval = 5000;
timer.Elapsed += timer_Elapsed;
timer.Start();
}
你必须使用Timer.Elapsed Event [ ^ ]
计时器类(System.Timers) [ ^ ]
参考这个例子
You will have to use Timer.Elapsed Event [^]
Timer Class (System.Timers)[^]
refer this example
using System;
namespace CPTemp
{
public class Class1
{
static void Main(string[] args)
{
int autoPollingTime = 1000; //Convert.ToInt32(configparams["AutoPollQueues"]);
System.Timers.Timer timer = new System.Timers.Timer();
timer.Interval = autoPollingTime;
if (autoPollingTime != 0)
{
timer.Elapsed += timer_Elapsed;
Console.WriteLine("Timer started...");
timer.Start();
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Press any key to stop the timer");
Console.ReadKey();
timer.Stop();
Console.WriteLine("\n Timer Stopped");
Console.ReadKey();
}
}
static void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
Check();
}
private static void Check()
{
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine("Timer running...");
}
}
}
//你可以使用TimerCallback
// you can use TimerCallback
Timer timer;
public void CallFirstTime()
{
TimerCallback tmCallback = your_method_name_to_call_for_every_5_second_like(interval_method);
timer = new Timer(tmCallback, "any parameter", 0, 5000);
}
private void interval_method(object _SessionID)
{
// this method call every 5 second
}
这篇关于我需要每5秒执行一次方法。如何使用计时器概念?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!