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

查看:23
本文介绍了如何在不使用睡眠的情况下在 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 += "
Thread Sleeps!";
 System.Threading.Thread.Sleep(4000);
 textBox1.Text += "
Thread awakens!";

好像是坚持先睡觉,再打印两行.

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

我认为目前我能说的就这些,但如果这仍然太模糊或冗长,请随时告诉我.

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 的情况下等待.您调用的方法(在 await 行上)也必须用 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. using 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 方法(可以使用 awaitTask.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 += "
Thread Sleeps!";
    await Task.Delay(3000);
    textBox1.Text += "
Thread awakens!";
}

您可以在此处阅读有关任务的更多信息.延迟等待.

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

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

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