Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I perform a short delay in C# without using 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.

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.

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

like image 509
user2706003 Avatar asked Aug 22 '13 05:08

user2706003


People also ask

Is there a delay function in C?

The delay() function is built upon a C library function called clock(). The clock() function returns a time value in clock ticks, which is based on the processor's speed. The value returned is of the clock_t variable type. You can use subsequent reads of the clock() function to determine elapsed time.

What is the easiest way to create a one second delay in a C program?

Insert, wherever you need your program to make a delay:sleep(1000); Change the "1000" to the number of milliseconds you want to wait (for example, if you want to make a 2 second delay, replace it with "2000".

How do you use delay function?

The way the delay() function works is pretty simple. It accepts a single integer (or number) argument. This number represents the time (measured in milliseconds). The program should wait until moving on to the next line of code when it encounters this function.

How do you add a delay?

To delay a function call, use setTimeout() function. functionname − The function name for the function to be executed. milliseconds − The number of milliseconds. arg1, arg2, arg3 − These are the arguments passed to the function.


1 Answers

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

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. 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.

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!"; } 

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

like image 170
Ohlin Avatar answered Sep 19 '22 03:09

Ohlin