[英]How I can waiting some time in c# (windows service)
I created windows service on C#. 我在C#上创建了windows服务。 For now I have methods for scanning DB.
现在我有扫描DB的方法。 I need call this method two times per minute.
我需要每分钟调用这个方法两次。 Actually I don't know method for waiting in windows service.
其实我不知道在Windows服务中等待的方法。 I tried Thread.Sleep... but nothing happened.
我试过Thread.Sleep ......但什么都没发生。 Please help me with this problem.
请帮我解决这个问题。
private int wait;
protected void Start()
{
wait = 1000;
while (true)
{
if (wait < 30000)
wait += wait;
//implement logic for waiting
Video video = new Video();
video.FindFileForConvert();
if (video.Path != null)
{
Console.WriteLine("video != null. video path = {0}", video.Path);
video.BeginConvertation();
video.DeleteOriginFile();
wait = 1000;
}
}
}
You should use System.Threading.Timer for the same. 您应该使用System.Threading.Timer 。 Since
Thread.sleep
is not a good practice atleast in some cases. 由于
Thread.sleep
在某些情况下至少不是一个好习惯。
public static int Main() {
/* Adds the event and the event handler for the method that will
process the timer event to the timer. */
myTimer.Tick += new EventHandler(TimerEventProcessor);
// Sets the timer interval to 5 seconds.
myTimer.Interval = 5000;
myTimer.Start();
// Runs the timer, and raises the event.
while(exitFlag == false) {
// Processes all the events in the queue.
Application.DoEvents();
}
return 0;
}
I may be mistaken, but I think that this code will help you resolve your problem. 我可能弄错了,但我认为这段代码可以帮助您解决问题。 DispatcherTimer
DispatcherTimer
DispatcherTimer dispathcerTimer = new DispatcherTimer();
dispathcerTimer.Interval = TimeSpan.FromMinutes(2);
dispathcerTimer.Tick += dispathcerTimer_Tick;
dispathcerTimer.Start();
void dispatcherTime_Tick(object sender, object e)
{
//function, which needs to be invoked every two minutes.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.