Click here to Skip to main content
15,889,096 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi guys,

Usually when Thread.Sleep(3000) is excuted, the thread will sleep for 3000 milliseconds, I would like to be able to specify the sleep time in either seconds or minutes.

How can I change Thread function Milli Seconds to Seconds or Minutes.

Regards
Vasanth
Posted
Updated 17-Aug-12 10:58am
v2

I have to admit that I think that Microsoft misnamed Sleep and it should have been SleepMilliseconds. However a way to do what you want without the complexity of dealing with TimeSpan is to create a new class to encapsulate the Sleep method.

namespace ConsoleApplication
{
	public static class Sleep
	{
		public static void Seconds(int seconds)
		{
			System.Threading.Thread.Sleep(seconds * 1000);
		}
		public static void Minutes(int minutes)
		{
			System.Threading.Thread.Sleep(minutes * 60000);
		}
	}
}


To provide more flexibility you could set the argument type to double.
 
Share this answer
 
v2
Comments
vasanthkumarmk 17-Aug-12 16:18pm    
Thanks a lot.......
Clifford Nelson 17-Aug-12 16:52pm    
Glad it is what you wanted. Using the TimeSpan as others suggested really was not that good a solution. You count have gotten the same thing by just multiplying, so why bother.

And yes, thanks so much for the vote and the accepting of the solution.
vasanthkumarmk 18-Aug-12 1:37am    
Another solution i needed for, How can i stop the for loop using other button click event. Here by i posted my question. iF 'For' loop is running my program other button controls not able to access. Please resolve that problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900