Viktor Ischenko
Viktor Ischenko

Reputation: 209

How I can waiting some time in c# (windows service)

I created windows service on C#. For now I have methods for scanning DB. I need call this method two times per minute. Actually I don't know method for waiting in windows service. I tried Thread.Sleep... but nothing happened. 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;
        }
    }
}

Upvotes: 5

Views: 4235

Answers (3)

Viacheslav
Viacheslav

Reputation: 151

I may be mistaken, but I think that this code will help you resolve your problem. 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.     
}

Upvotes: 0

dotmido
dotmido

Reputation: 1384

You may use Timer

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

Upvotes: 1

user2166576
user2166576

Reputation:

You should use System.Threading.Timer for the same. Since Thread.sleep is not a good practice atleast in some cases.

Upvotes: 3

Related Questions