FOLLOW US
softpcapps Software CODE HELP BLOG

Shareware and free Open Source Windows Software Applications and free Online Tools

How to wait for N milliseconds without freezing the thread

Sometimes it is necessary to wait for some time. This is necessary for example to wait for a task to complete.
The problem is that if we use System.Thread.Thread.Sleep the thread freezes and GUI changes are not done.
Threfore we found another way. Use the following method to wait for N milliseconds without freezing the thread :
	

	public static void WaitNMSeconds(int mseconds)
	{
		if (mseconds < 1) return;
		DateTime _desired = DateTime.Now.AddMilliseconds(mseconds);
		while (DateTime.Now < _desired)
		{
			System.Windows.Forms.Application.DoEvents();
		}
	}