Alexandera MacQueen
Alexandera MacQueen

Reputation: 99

c# Function work with Timer(AutoFunction)

I do have one checking function that will run once opening the application. How to make it Auto Function like every 20 seconds run the function?

Main()
{
  Checking();
}

public void Checking() // run this function every 20 seconds
{ // some code here   
} 

Upvotes: 0

Views: 760

Answers (2)

Sami
Sami

Reputation: 8419

Main()
{
   Timer tm = new Timer();
   tm.Interval = 20000;//Milliseconds
   tm.Tick += new EventHandler(tm_Tick);
   tm.Start();
}
void tm_Tick(object sender, EventArgs e)
{
   Checking();       
}

public void Checking()
{
   // Your code
} 

Upvotes: 0

bkmtan
bkmtan

Reputation: 101

You can use the C# Timer class

public void Main()
{
    var myTimer = new Timer(20000);

    myTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);

    myTimer.Enabled = true;

    Console.ReadLine();
}


private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
        Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime);
}

Upvotes: 2

Related Questions