This post originated from an RSS feed registered with .NET Buzz
by Eric Gunnerson.
Original Post: A lock statement with timeout...
Feed Title: Eric Gunnerson's C# Compendium
Feed URL: /msdnerror.htm?aspxerrorpath=/ericgu/Rss.aspx
Feed Description: Eric comments on C#, programming and dotnet in general, and the aerodynamic characteristics of the red-nosed flying squirrel of the Lesser Antilles
Ian Griffiths comes up with an interesting way to use IDisposable and the “using“ statement to get a very of lock with timeout.
I like the approach, but there are two ways to improve it:
1) Define TimedLock as a struct instead of a class, so that there's no heap allocation involved.
2) Implement Dispose() with a public implementation rather than a private one. If that's the case, the compiler will call Dispose() directly, otherwise it will box to the IDisposable interface before calling Dispose().