0

I've got a few lines of codes for example:

MessageBox.Show("1");

Sleep(1000);

MessageBox.Show("2");

Sleep(1000);

MessageBox.Show("3");

And I want to make a pause of 1 second before continuing to the next code line without using Thread.Sleep; because it freezes the whole Form and terminates the lines of codes that was supposed to be executed before the sleep code. I don't use message boxes for this, I used just for an example I know it would of work with those.

With the lines I originally use it doesn't work. Is there any other alternative to wait for 1s before continuing running the codes? Thanks.

Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
  • 4
    yes make your method async and use `await Task.Delay(1000)` – Mong Zhu May 09 '19 at 09:39
  • 2
    *"Is there any other alternative"* - many. `async/await` is probably the best. If you can split operation into steps then `Timer` can do. If you want to avoid UI freezing, then look into multithreading: `Task`, `Thread`, `BackgroundWorker`, etc. – Sinatr May 09 '19 at 09:43
  • you should provide more information. Please post the signature of your method and tell us which UI technology you are using. WPF, WinForms ect... – Mong Zhu May 09 '19 at 09:48

2 Answers2

3

yes make your method async and use await Task.Delay(1000)

public async Task MyAsyncMethod()
{
    //Action 1  
    await Task.Delay(1000);

    //Action 2  
    await Task.Delay(1000);


    //Action 3  
    await Task.Delay(1000);
}

EDIT:

If using Winforms this async pattern has to go to the highes method in the hierarchy. So let's say you really have a button click event in which you want to call this method. Then you have to make the click method async!

private async void button2_Click(object sender, EventArgs e)
{
    Console.WriteLine("STUFF Before");
    await MyAsyncMethod();
    Console.WriteLine("STUFF @ the END");
}

public async Task MyAsyncMethod()
{
    //Action 1  
    Console.WriteLine("Action 1 ");
    await Task.Delay(1000);

    //Action 2  
    Console.WriteLine("Action 2 ");
    await Task.Delay(1000);


    //Action 3  
    Console.WriteLine("Action 3 ");
    await Task.Delay(1000);
}

To realize this, you need to declare the return type of the MyAsyncMethod as Task:

public async Task MyAsyncMethod()    
               ^
               !
Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
2

Edit:

After the comments made in the response, the best solution is to use await Task.Delay(time);

private async void AsyncMethod()
{
    //do what you want here
    await Task.Delay(1000);
}

This way is a bad practice like @MickyD said in his comment:

It causes your GUI application to become re-entrant. It is a throw-back to Visual Basic 6 and bad coding practices.

int seconds = 2;
if (seconds < 1) return;
DateTime _desired = DateTime.Now.AddSeconds(seconds);
while (DateTime.Now < _desired)
{
   System.Windows.Forms.Application.DoEvents();
   //do whatever you want in this 2 seconds
}
Mikev
  • 2,012
  • 1
  • 15
  • 27
  • 1
    Dude, Thank you so much! I was searching for hours and nothing worked and I haven't seen any alternative code. I pasted your code into a method and made a var inside the () so everytime I wanna pause I just use waitSec(4); and it waits the amount of seconds inside the (); Awesome! – GucciHoodie May 09 '19 at 09:52
  • 2
    _**Never never never never never never never**_ use `Application.DoEvents`!. It causes your GUI application to become re-entrant. It is a throw-back to Visual Basic 6 and bad coding practices. **Mong's** [answer](https://stackoverflow.com/a/56056604/585968) is the way to go –  May 09 '19 at 10:03
  • @MickyD What's the main difference between Application.DoEvents and Async Task.Delay? – Mikev May 09 '19 at 10:04
  • @Mikev - [Use of Application.DoEvents()](https://stackoverflow.com/q/5181777/1260204) – Igor May 09 '19 at 10:05
  • 2
    `Application.DoEvents()` has it's own Windows message pump processing and will process things like mouse clicks and keyboard. The problem is, most of the time you are already in an event handler when you decided to call `DoEvents()` **but** you haven't returned yet from the event handler. Now there is the possibility of processing another event from a menu/toolbar before the first has finished. It will lead to unexpected application state. The effect is similar to if you applied multithreading to code which isn't thread-safe –  May 09 '19 at 10:11