Forget 42, –1 is My New Answer to Life

I’ve just stumbled upon the next code statement – Thread.Sleep(-1). It left me wondering what was happening there since MSDN tells you nothing about a –1 value for the milliseconds parameter:

“The number of milliseconds for which the thread is blocked. Specify zero (0) to indicate that this thread should be suspended to allow other waiting threads to execute. Specify Infinite to block the thread indefinitely.“ – System.Threading.Thread.Sleep, MSDN

To check that out, I opened the IronRuby interactive console and filled in System::Threading::Thread.Sleep(-1) and hit Enter just to find out that this call blocks the thread indefinitely. Could it be? –1 is Infinite?

Reflector to our aid! oh wait, RedGate now charges money for it and had planted a time bomb inside the free version which made it stop working. grrrr
ILSpy to our aid! (I highly recommend ILSpy as a Reflector alternative… very similar, free, oss… great community effort!)

Anyway, ILSpy proved my concerns:

System.Threading.Timeout.Infinite Value

So… –1 actually represents Infinity (when it comes to threading in .NET). And Infinity is much cooler than 42, hence –1 is the new answer to life.
Q.E.D.

All the best,
Shay.




Comments

April 25. 2011 02:05 PM

Falko Janak

Hello Shay,
nice hint. Can you give an example when to block a thread indefinitely?
Thx,
Falko

Falko Janak

April 25. 2011 03:11 PM

Shay Friedman

I ran into this particular code statement in Microsoft's implementation of the RoleEntryPoint class (the base class for Azure roles). In general, you'd want to use it when you're doing operations not on the main thread and you want your application to remain active.

Shay Friedman

April 27. 2011 10:41 AM

Oracle E-Business Suite Training

What do you mean by time bomb in the free version of Redgate? Does ILSpy provide all the functionalities that Redgate provides.?

Oracle E-Business Suite Training

April 28. 2011 03:04 PM

Alon

This came from the INFINITE definition in Win32 which is the unsigned value 0xFFFFFFFF == -1.

Alon

Alon

December 21. 2012 02:14 PM

bd domain

Hosting Bangladesh, best web design company in bangladesh, Reseller Hosting Bangladesh-  Domain Bangladesh

bd domain

December 25. 2012 05:01 PM

Domain Registration Bangladesh

nice site dear

Domain Registration Bangladesh

January 28. 2013 11:07 AM

UK discount codes

Brilliant! Thx for experimenting and sharing your knowledge to us. Keep it up.

UK discount codes

January 31. 2013 03:35 AM

Jeremy

I cannot stop laughing and this article is getting harder and harder to read. I understand your predicament well; I had the same problem not so long ago. It is really important that you're always <a href="www.carriehq.net/...igence.html">Analyzing Data</a> and identifying the errors in the code. I know that this will take up a lot of time, but there is nothing more fulfilling then solving problems and coming up with solutions, at least for me.

Jeremy

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading



Subscribe Subscribe

That's Me!

Hi! I'm Shay Friedman
I'm Shay Friedman - a Visual C#/IronRuby MVP, a consultant and instructor of .NET technologies, author, speaker and new technologies freak
More about me

Contact Me

> Contact page
> Twitter: @ironshay
> LinkedIn profile

Search

Hosted By

I'm hosting this site on Arvixe and I'm very happy with it.
If you're looking for ASP.NET hosting, I highly recommend it
(and if you order from this link I also get some beer money!)
Web Hosting