4

C#でWindowsサービスを作成しました。今のところ、DBをスキャンする方法があります。このメソッドを1分間に2回呼び出す必要があります。実は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

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 に答える