如何在不使用睡眠的情况下在C#中执行短暂延迟? [英] How can I perform a short delay in C# without using sleep?

查看:86
本文介绍了如何在不使用睡眠的情况下在C#中执行短暂延迟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对编程非常陌生,并且到目前为止,我已经学得足够好了,但是我仍然无法理解延迟我想要的方式的想法。我正在使用一种涉及战斗系统的Windows窗体应用程序进行某种测试游戏。在其中,我想制作一个每两秒钟执行一次操作的NPC。问题是,我还希望允许玩家在攻击之间进行交互。 Thread.sleep确实对我似乎不起作用,不仅因为我不知道如何进行多线程,而且每当我尝试运行它时,都说像这样:

I'm incredibly new to programming, and I've been learning well enough so far, I think, but I still can't get a grasp around the idea of making a delay the way I want. What I'm working on is a sort of test "game" thingy using a Windows forms application that involves a combat system. In it, I want to make an NPC that does an action every couple of seconds. The problem is, I also want to allow the player to interact between attacks. Thread.sleep really doesn't seem to work for me not only because I don't know how to multithread, but whenever I try to run it, say, like this:

 textBox1.Text += "\r\nThread Sleeps!";
 System.Threading.Thread.Sleep(4000);
 textBox1.Text += "\r\nThread awakens!";

似乎坚持先睡觉,然后打印两行。

It seems to insist on sleeping first, then printing both lines.

我想我现在只能说这些,但是如果仍然太含糊或罗word,请随时告诉我。

I think that's all I can say at the moment, but if that's still too vague or wordy, feel free to tell me.

简而言之,在C#中,我想在运行之前进行一些延迟,但同时仍允许用户交互。

In short, In C# I want to make something delay before running but at the same time still allow user interaction.

推荐答案

如果您使用的是.NET 4.5,则可以使用新的async / await框架进行睡眠而无需锁定线程。

If you're using .NET 4.5 you can use the new async/await framework to sleep without locking the thread.

它的工作原理是使用 async 关键字标记需要异步操作的函数。这只是对编译器的提示。然后,在希望代码异步运行的行上使用 await 关键字,程序将等待而不会锁定线程或UI。您调用的方法(在等待行上)也必须标记为 async 关键字,并且通常以Async结尾,如 ImportFilesAsync

How it works is that you mark the function in need of asynchronous operations, with the async keyword. This is just a hint to the compiler. Then you use the await keyword on the line where you want your code to run asynchronously and your program will wait without locking the thread or the UI. The method you call (on the await line) has to be marked with an async keyword as well and is usually named ending with Async, as in ImportFilesAsync.

在您的示例中,您需要做的是:

What you need to do in your example is:


  1. 制作确保您的程序具有 .Net Framework 4.5 作为目标框架

  2. async 关键字(请参见下面的示例)

  3. 使用System.Threading.Tasks添加

  1. Make sure your program has .Net Framework 4.5 as Target Framework
  2. Mark your function that needs to sleep with the async keyword (see example below)
  3. Add using System.Threading.Tasks; to your code.

您的代码现在可以使用 Task.Delay 方法了 System.Threading.Thread.Sleep 方法的方法(可以在上使用 await Task.Delay ,因为 Task.Delay 在其定义中标记为 async )。

Your code is now ready to use the Task.Delay method instead of the System.Threading.Thread.Sleep method (it is possible to use await on Task.Delay because Task.Delay is marked with async in its definition).

private async void button1_Click(object sender, EventArgs e)
{
    textBox1.Text += "\r\nThread Sleeps!";
    await Task.Delay(3000);
    textBox1.Text += "\r\nThread awakens!";
}

在这里您可以阅读有关 Task.Delay 等待

Here you can read more about Task.Delay and Await.

这篇关于如何在不使用睡眠的情况下在C#中执行短暂延迟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆