11

First off, I am a beginner in C# and I would like to make this:

class2.method_79(null, RoomItem_0, num, num2, 0, false, true, true);
System.Threading.Thread.Sleep(250);
class2.method_79(null, RoomItem_0, num, num4, 0, false, true, true);
System.Threading.Thread.Sleep(300);
class2.method_79(null, RoomItem_0, num, num6, 0, false, true, true);

But this solution freezes the UI, how could I make the second event occur 250ms after the first etc without freezing the UI?

aevitas
  • 3,753
  • 2
  • 28
  • 39
user3640047
  • 177
  • 1
  • 2
  • 7
  • 1
    Look up [Multithreading](http://msdn.microsoft.com/en-us/library/aa645740(v=vs.71).aspx). – Sam Jun 10 '14 at 08:37
  • Do not block the UI thread. Use a worker thread with a sleep. (Note that you can also better use something like a timer). Please read this http://stackoverflow.com/a/11576659/296526 including the link in the answer. – Myrtle Jun 10 '14 at 08:37
  • 1
    You could also make the method `async` and use `await Task.Delay(250)`. – Nico Schertler Jun 10 '14 at 08:38
  • possible duplicate of [Threading in c#](http://stackoverflow.com/questions/1379240/threading-in-c-sharp) – Sam Jun 10 '14 at 08:40
  • Finally I used the async method with await task.delay it works perfectly thanks!!! – user3640047 Jun 10 '14 at 10:04
  • When everyone Not want top make method async it ist possible to use '''Task.Delay(250).Wait()''' – Marcus Apr 22 '20 at 05:13

6 Answers6

24

The simplest way to use sleep without freezing the UI thread is to make your method asynchronous. To make your method asynchronous add the async modifier.

private void someMethod()

to

private async void someMethod()

Now you can use the await operator to perform asynchronous tasks, in your case.

await Task.Delay(milliseconds);

This makes it an asynchronous method and will run asynchronously from your UI thread.

Note that this is only supported in the Microsoft .NET framework 4.5 and higher.

.

Stella
  • 498
  • 4
  • 15
  • If awaitable type is not needed by` class2.method_79(null, RoomItem_0, num, num4, 0, false, true, true);` and it is not because await void does not returns anything then the timespan between those method will never be as miliseconds given... – Sebastian Xawery Wiśniowiecki Apr 14 '20 at 18:39
3

You could use a Dispatcher Timer to time your execution of methods..

bit
  • 4,407
  • 1
  • 28
  • 50
0

You are in the UI thread when you call .Sleep();.

That's why it's freezing the UI. If you need to do this without freezing the UI you would need to run the code in separate threads.

DGibbs
  • 14,316
  • 7
  • 44
  • 83
  • How could I do to apply it in my code ? I am beginner, and never seen how to use multi-threading before... – user3640047 Jun 10 '14 at 09:51
  • @user3640047 You will have to learn. [Here's a good starting point.](http://msdn.microsoft.com/en-us/library/aa645740(v=vs.71).aspx) – DGibbs Jun 10 '14 at 09:52
0

Run your time consuming tasks on separate thread. Avoid time consuming tasks and Thread.Sleep() on UI thread.

Ricky
  • 2,323
  • 6
  • 22
  • 22
0

Try this code

public static void wait(int milliseconds)
        {
            System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
            if (milliseconds == 0 || milliseconds < 0) return;
            timer1.Interval = milliseconds;
            timer1.Enabled = true;
            timer1.Start();
            timer1.Tick += (s, e) =>
            {
                timer1.Enabled = false;
                timer1.Stop();
            };
            while (timer1.Enabled)
            {
                Application.DoEvents();
            }
        }
Kelin
  • 349
  • 2
  • 5
-1

Put the function in a Task.Factory.StartNew and, after that, use Thread.Sleep().

Example:

private void btnExample_Click(object sender, EventArgs e)
{
    System.Threading.Tasks.Task.Factory.StartNew(() =>
    {
        System.Threading.Thread.Sleep(2000);
        MessageBox.Show("First message after one second without freezing");
        System.Threading.Thread.Sleep(2000);
        MessageBox.Show("Second message after one second without freezing");
        System.Threading.Thread.Sleep(2000);
        MessageBox.Show("Third message after one second without freezing");
    });
}

Testing video

TobyAdd
  • 1
  • 1