Ren
Ren

Reputation: 775

Call function after a period of time in C#

I have a c# code with call function something like:

private void cmdCommand_Click(object sender, EventArgs e)
    {
       m_socClient.Close();
    }

But now i want the m_socClient.Close(); function to be called after maybe 30 second. So is that possible?

Upvotes: 3

Views: 1684

Answers (1)

Selman Genç
Selman Genç

Reputation: 101681

Yes, using async / await feature:

private async void cmdCommand_Click(object sender, EventArgs e)
{ 
    await Task.Delay(30000);
    m_socClient.Close();
}

You can also do that using Thread.Sleep but it will block your UI thread and your window won't be responsive until the task is finished.

Upvotes: 11

Related Questions