4

我在 C# 上创建了 Windows 服务。现在我有扫描数据库的方法。我需要每分钟调用此方法两次。其实我不知道在 Windows 服务中等待的方法。我试过 Thread.Sleep ......但什么也没发生。请帮我解决这个问题。

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;
        }
    }
}
4

3 回答 3

3

您应该使用System.Threading.Timer相同。因为Thread.sleep至少在某些情况下不是一个好习惯。

于 2013-03-12T09:42:55.137 回答
1

您可以使用计时器

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;
}
于 2013-03-12T09:43:40.177 回答
0

我可能弄错了,但我认为这段代码可以帮助您解决问题。调度器定时器

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.     
}
于 2013-03-12T10:04:22.130 回答