简体   繁体   English

Thread.Sleep() 不冻结 UI

[英]Thread.Sleep() without freezing the UI

First off, I am a beginner in C# and I would like to make this:首先,我是C#的初学者,我想做这个:

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?但是这个解决方案冻结了 UI,我怎样才能让第二个事件在第一个事件等 250 毫秒后发生而不冻结 UI?

The simplest way to use sleep without freezing the UI thread is to make your method asynchronous.在不冻结 UI 线程的情况下使用睡眠的最简单方法是使您的方法异步。 To make your method asynchronous add the async modifier.要使您的方法异步,请添加async修饰符。

private void someMethod()

to

private async void someMethod()

Now you can use the await operator to perform asynchronous tasks, in your case.现在,根据您的情况,您可以使用await运算符执行异步任务。

await Task.Delay(milliseconds);

This makes it an asynchronous method and will run asynchronously from your UI thread.这使它成为一种异步方法,并将从您的 UI 线程异步运行。

Note that this is only supported in the Microsoft .NET framework 4.5 and higher.请注意,这仅在 Microsoft .NET Framework 4.5 及更高版本中受支持。

. .

您可以使用Dispatcher Timer来计时方法的执行。

You are in the UI thread when you call .Sleep();当您调用.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.如果您需要在不冻结 UI 的情况下执行此操作,则需要在单独的线程中运行代码。

Run your time consuming tasks on separate thread.在单独的线程上运行耗时的任务。 Avoid time consuming tasks and Thread.Sleep() on UI thread.避免在 UI 线程上执行耗时的任务和Thread.Sleep()

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();
            }
        }

Make an async function.做一个async函数。 Put the function in a Task.Factory.StartNew and, after that, use Thread.Sleep() .将该函数放入Task.Factory.StartNew中,然后使用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");
    });
}

测试视频

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM