1

So what i want to make is that when you click a button it waits a certain time then shows a messagebox. I am using windows forms apps so yeah will appreciate if you help me.

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Example");
        }
    }
}

Edit: What i did was:

 private async void button1_Click(object sender, EventArgs e)
        {
            await Task.Delay(Amount of milliseconds here);
            MessageBox.Show("Example");
        }
Kristian
  • 29
  • 2
  • 1
    Thread.Sleep(2000) will wait 2s I think, but it may block the ui durng that time – Pierre Michel Jan 24 '21 at 10:13
  • 2
    Does this answer your question? [Sleep without freezing UI](https://stackoverflow.com/questions/24136390/thread-sleep-without-freezing-the-ui) and [Wait some seconds without blocking UI](https://stackoverflow.com/questions/22158278/wait-some-seconds-without-blocking-ui-execution) and [How to correctly pause/delay Forms application](https://stackoverflow.com/questions/34240671/how-to-correctly-pause-delay-windows-forms-application) and [How can I perform a delay without using sleep?](https://stackoverflow.com/questions/18372355/how-can-i-perform-a-short-delay-in-c-sharp-without-using-sleep) –  Jan 24 '21 at 10:18
  • Use a [Timer](https://learn.microsoft.com/dotnet/desktop/winforms/controls/run-procedures-at-set-intervals-with-wf-timer-component) with `Interval = 2000` for example for 2s ? Therefore the event handler method may be `private void Timer1_Tick(object Sender, EventArgs e) { Timer1.Enabled = false; MessageBox.Show("Example"); }`. The timer can be dynamically created instead of adding one using the Form Designer... Does it fit or do you want threading? These allow to not block the UI, unless you need that, so use Sleep. –  Jan 24 '21 at 10:24
  • Your solution with `await Task.Delay` does not work? What does it do? – Harald Coppoolse Jan 26 '21 at 10:01

0 Answers0