I’m trying to create a realistic console type game with C#, and what I need it to do is append text like this:
hi
hello
awesome
nice
but instead of it going ALL at once, I want it to do this:
hi
(2 second pause)
hello
(4 second pause)
awesome
(1 second pause)
nice
I have tried this:
delegate void _pause(int time);
void pause(int time)
{
if (txtInput.Text != "")
{
txtInput.Clear();
}
Stopwatch s = new Stopwatch();
s.Start();
while (s.Elapsed < TimeSpan.FromSeconds(time))
{
}
s.Stop();
return;
}
And then I can call it like this Invoke(new _pause(pause),3); but that basically freezes the thread! So it pastes all at once anyway, just a delay before
the montage of paste!
How can I do a smooth delay type system like I have described?
You should avoid busy-waiting and instead use a timer such as
Task.Delay.One possible way of doing that is by leveraging C# 4.5’s new async feature:
By using the async syntax you are instructing the machine to run your code interleaved with other code – so that it can still do screen updates in the meantime, for instance. It automatically returns to the same SynchronizationContext, so in your case since you started on the GUI thread, you’ll stay on it; this is important since only that thread may alter your text-box.
To summarize why you want to do it similar to this like this:
If you need to support .NET 2.0, you may want to look at the windows forms
Timer. In short, this exposes event handlers you can hook which fire after a delay. However, as the example on the MSDN page illustrates, it’s not as easy to use and will take much more manual orchestration.Additionally, you could use a thread. If you do so, you are responsible for ensuring thread safety: you may NOT execute
txtInput.Appendon any thread other than the GUI thread:This model is almost as simple as the async model. Two pitfalls are that you should avoid busy waiting (use Thread.Sleep) and that you must make sure you properly use
BeginInvokefor all UI-affecting code. It’s possibly even slightly faster thanasync. However, threads have a fairly large overhead (mostly in terms of memory), so this solution won’t scale to large amounts of delayed interaction.Summary
asyncif you can.Timer, which is a hassle.