Bizzie
Bizzie

Reputation: 47

How can I make a piece of code repeat every x seconds?

I'm trying to get a specific piece of code to repeat.

I would like it so that when I press a button, it opens an application and then closes it 10 seconds later, only to reopen it again. I believe I would have to use 2 timers, but I still can't figure out what code I should use to set an on timer end (or something like that) to run the specified code.

Upvotes: 1

Views: 361

Answers (1)

John Wu
John Wu

Reputation: 52240

If you make your click handler async you can just use Task.Delay.

public async void MyButton_Click(object sender, EventArgs 3)
{
    OpenApplication();
    await Task.Delay( 10 * 1000 ); //10,000 milliseconds a.k.a. 10 seconds
    CloseApplication();
    OpenApplication();
}

If you don't know how to "close" an application, see this related question.

Upvotes: 3

Related Questions