General
lock(typeof(MyLockedType)) is bad, mmmm'ok?

Seems that MS have been promoting bad practice in some of their documentation...

http://msdn.microsoft.com/library/default.asp?url=/archive/en-us/dnaraskdr/html/askgui06032003.asp

Long and the short is don't lock on a type, so:

public class MyLockedType
{
  public static void DoWork()
  {
     lock(typeof(MyLockedType))
     {
       ...do something here
     }
  }
}

is bad and should be rewritten as:

public class MyLockedType
{
  private static object mLockObj = new object();

  public static void DoWork()
  {
     lock(mLockObj)
     {
       ...do something here
     }
  }
}

There's more details in the article, but the main problem boils down to not having control over the MyLockedType to the extent that code outside of the class can lock on the type. Not only does this break the encapsulation but could lead to some very fun debugging.

Posting images into your blog

Was asked how I got the image into my previous post. Here you go:

  • Add a gallery
  • Add the image to the gallery
  • View the image in the gallery and get the URL
  • Paste the URL in an img tag (watch out for a double slash in the URL).
  • Voila! Images in your blog
Add Comment Filed Under [ General ]