sukkis
sukkis

Reputation: 322

How to invoke a function on a timer

Goal is to save file continuously but I have no idea how to make timer in C#. So how can I do this JavaScript known function

setInterval(function(){ recurring_task(); }, 3000);

In C#?

Upvotes: 1

Views: 2414

Answers (3)

Mike Nakis
Mike Nakis

Reputation: 61969

The Thread.Sleep(_specifiedTimeInMs) approach suggested in another answer will not work unless you spawn a separate thread to perform your repeated task.

The Task.Delay().ContinueWith() approach saves you from having to manually start a separate thread, but it will be using a separate thread behind the scenes.

The problem with using a separate thread is synchronization: if you go this way, you will need to make sure that it is safe for your recurring task to try to access data structures at the same time that your main (gui) thread is also trying to access them.

If your recurring task is lengthy, then you might not have any other option but to perform your recurring task in a separate thread, so as not to freeze your gui thread while the recurring task is doing its thing. But if your recurring task is fast, then it is a lot easier to try and do it in the gui thread using a regular winforms timer.

This sample code is taken straight from Microsoft documentation, "How to: Run Procedures at Set Intervals with the Windows Forms Timer Component" which is one of the first results that you receive if you google "winforms timer".

It tracks the time of day in one-second increments. It uses a Button, a Label, and a Timer component on a form. The Interval property is set to 1000 (equal to one second). In the Tick event, the label's caption is set to the current time. When the button is clicked, the Enabled property is set to false, stopping the timer from updating the label's caption. The following code example requires that you have a form with a Button control named Button1, a Timer control named Timer1, and a Label control named Label1.

private void InitializeTimer()  
{  
    // Call this procedure when the application starts.  
    // Set to 1 second.  
    Timer1.Interval = 1000;  
    Timer1.Tick += new EventHandler(Timer1_Tick);  

    // Enable timer.  
    Timer1.Enabled = true;  

    Button1.Text = "Stop";  
    Button1.Click += new EventHandler(Button1_Click);  
}  

private void Timer1_Tick(object Sender, EventArgs e)     
{  
   // Set the caption to the current time.  
   Label1.Text = DateTime.Now.ToString();  
}  

private void Button1_Click(object sender, EventArgs e)  
{  
  if ( Button1.Text == "Stop" )  
  {  
    Button1.Text = "Start";  
    Timer1.Enabled = false;  
  }  
  else  
  {  
    Button1.Text = "Stop";  
    Timer1.Enabled = true;  
  }  
}  

Upvotes: 1

Guillermo Gerard
Guillermo Gerard

Reputation: 892

You can use something like this:

public void foo()
{
    Task.Delay(3000).ContinueWith(t=> bar());
}

public void bar()
{
    // do stuff
    foo();   //This will run the next bar() in 3 seconds from now 
}

Upvotes: 0

Michał Turczyn
Michał Turczyn

Reputation: 37337

You can use while loop with some flag that can be set globally to stop loop.

Something as:

// define some flag in a class
// in case of multithreaded app, use volatile keyword to make it thread safe
private bool _globalFlag = true;
// then in your method use the flag
while(_globalFlag)
{
  // save your file
  // consider starting this method in another thread, so your main thread won't get blocked
  Thread.Sleep(_specifiedTimeInMs);
}

Upvotes: 0

Related Questions