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.
You can probably use timers : http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx
Timers can provide you a precision up to 1 millisecond. Depending on the tick interval an event will be generated. Do your stuff inside the tick event.
Sorry for awakening an old question like this. But I think what the original author wanted as an answer was:
You need to force your program to make the graphic update after you make the change to the textbox1. You can do that by invoking
Update();
Normally this will be done automatically when the thread is done. Ex, you press a button, changes are made to the text, thread dies, and then
.Update()
is fired and you see the changes. (I'm not an expert so I cant really tell you when its fired, but its something similar to this any way.)In this case, you make a change, pause the thread, and then change the text again, and when the thread finally dies the
.Update()
is fired. This resulting in you only seeing the last change made to the text.You would experience the same issue if you had a long execution between the text changes.
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 theawait
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 anasync
keyword as well and is usually named ending with Async, as in ImportFilesAsync.What you need to do in your example is:
async
keyword (see example below)using System.Threading.Tasks;
to your code.Your code is now ready to use the
Task.Delay
method instead of theSystem.Threading.Thread.Sleep
method (it is possible to useawait
onTask.Delay
becauseTask.Delay
is marked withasync
in its definition).Here you can read more about Task.Delay and Await.
By adding
using System.Timers;
to your program you can use this function:Delay is a function and can be used like: