Thursday, July 15, 2010

Old dogs can learn new tricks

About a year ago I wrote about using generics and creating thread-safe access to the Web Cache in ASP.NET. In that example I used a pattern that I had copied from some example code and used the classes Type itself to lock the block of code.

lock (typeof(CacheUtils)) {...}

I've since learned this is a bad idea (not a Really Bad Idea but bad enough). After reading articles like Don't Lock Type Objects and Safe Thread Synchronization I've realized the errors of my ways. The better pattern is to use an internal lightweight objects. For example:

private static readonly object locker = new object();
lock (locker) {...}

Tuesday, April 13, 2010

JS editing in VS 2008

When you have JavaScript in an .aspx file Visual Studio 2008 will list all of the functions in a drop down at the top of a page. However once you move the JS to a separate file VS suddenly loses this ability. You can trick it into regaining this feature with the following steps.

In the .js file add the following to the top and bottom of the file:
//<html xmlns="http://www.w3.org/1999/xhtml"><head><title></title><script type="text/javascript">
…
//</script></head><body></body></html>

And then in Tools -> Options -> File Extentions map js to the HTML editor: