FOLLOW US
4dots Software CODE BLOG

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();
		}
	}        


 

Please Donate!

Please Share!