Me like!
int sleepTime = 1.Seconds(); while (condition) { Thread.Sleep(sleepTime*=2); if(sleepTime>10.Minutes()) { //give up return; } } DoWork();
And no, those extensions are not defined under the System namespace.
Ruby on Rails had those years ago!
nice one
(int sleepTime = 1.Seconds();
while (condition)
{
Thread.Sleep(sleepTime*=2);
if(sleepTime>10.Minutes())
//give up
return;
}
DoWork();).DontLike();
No no. Wait.
You'll wait 17 minutes until giving up.
You'll wait 1 second, then another 2, then another 4, after 9 iterations, you'll wait 8.5 minutes. After the 10th - you've waited 17 minutes.
Adrian - I really don't care about the algorithm. It's the extenstion methods thing.
And if we are talking algorithm - it doesn't matter - the time cap is just a way to "give up" after some amount of time. I don't plan on really getting to it, nor do I mind if it's not exact.
It could have been written with a timer which simply times how long it was "trying", if that was an issue.
What about SCMM?
Ariel - I don't care that you don't care about the algorithm. I'm adding interesting content to your blog.
Thinking about the trivial algorithm and finding out that it has a bug - is more interesting than the extension method.
SCMM - You'll be the first to know about it - and get a free life-time license.
Pingback from Dew Drop – December 27, 2009 | Alvin Ashcraft's Morning Dew
The only thing I don't know if I like is that it relies on the fact that many of the methods that will be consuming the return from these extensions are geared towards milliseconds. If you have a method that takes a timeout in seconds instead of milliseconds, you could inadvertently introduce a bug.
Perhaps (though long winded as it may be) something like:
10.MinutesToMs()
To avoid confusion?
or 10.Minutes(Time.Milliseconds)
I know they're a bit longer, but would reduce any ambiguity.
Aother thing you could do off integer would be to create an extension to in effect power a foreach loop:
10.Times(() => { Console.WriteLine("Hello"); });
James -
I agree that the unit of measurement is unclear, and that's why the "contextual" is in the title.
I wouldn't like to have an extension method on int called "Minutes", but is the scope of my class here (or even in the scope of System.Threading") - this could be the correct usage.
I totally agree that this should not be available everywhere.