4.5.11

Navigate to Definition in Visual Studio 2010

Visual Studio has a window that will show you the source code of any selected method, class, property or field. It is called the Code Definition window. You can display this window by using the menu and going to View->Code Definition Window (or Ctrl+\,Ctrl+D). This displays under the code editor by default.



You can use this in sync with the Call Hierarchy Window to quickly navigate to interface implementations. By right clicking on an item in the Code Defintion Window and choosing View Call Heirarchy (or Ctrl+K, Ctrl+T). When you are viewing the call heirarchy on an abstract item, a folder displays called Implements. In this folder will be displayed all the classes that implement this method, and clicking on one of them will take you to the definition indicated.

Summary:


  1. highlight the definition you want to see. It automatically shows up in the Code Definition Window.
  2. Press Ctrl+\,Ctrl+D to navigate to the Code Definition Window
  3. Finally, press Ctrl+K, Ctrl+T to show the Code Hierarchy Window

21.4.11

ASP.NET and the ThreadStaticAttribute do not work well together.


ASP.NET has its own thread pool. In an effort to increase performance, the ASP.NET runtime will return threads to the thread pool if a process is blocking so that it can be handed off to other incoming requests. When the blocking process returns, ASP.NET will randomly pick a thread in the pool and that process can continue executing. The interesting thing is that this may not be the same process that the request originally began executing on. Most of the time you never even notice this is happening. That is until you introduce the ThreadStaticAttribute.



The ThreadStaticAttribute causes feilds marked with the static keyword to be unique to each thread. In other words each thread gets its own copy of that static value. It has a unique quirk. You cannot set its value via a static constructor, or by in place
declaration and assignment like so:

// Example 1
public class Baz
{

 // all of these only get executed once 
 // when the .NET runtime loads the class

 [ThreadStatic]
 static int foo = 1;

 [ThreadStatic]
 static int bar;

 static Baz()
 {
  bar = 3;
 }
}



This is because the .NET runtime only executes static constructors and in place assignment once when the class is loaded, therefore you cannot be sure if your thread is the one that loaded this class. You can however initialize it in any operation you call which is usually static itself. The following example uses two thread static fields to store a resource that needs to be refreshed hourly. The refresh is lock free at the expense that each thread has its own copy. This copy is stored in what is commonly referred to as thread local storage.

// Example 2
public class Baz
{
 [ThreadStatic]
 static DateTime _lastLoaded;

 [ThreadStatic]
 static Resource _resource;

 static TimeSpan timeout = new TimeSpan(1, 0, 0); // one hour

 public static Resource ProcessStuff()
 {
  if (_lastLoaded.Add(timeout) < DateTime.Now)
  {
   _resources = GetResourcesFromExternalLocation();
   _lastLoaded = DateTime.Now;
  }
  return _resource;
 }
}
The thread local storage ensures that the values in _resource and _lastLoaded are the values created by the thread executing ProcessStuff(). This is rather efficent. However when ASP.NET joins the picture, meyham ensuse.

Remember from earlier that ASP.NET will recycle threads. Basically when you begin an operation, at any point, it could be put to sleep, and its thread recycled. When ASP.NET decides it time to wake up the operation it randomly chooses a thread from the pool to resume execution of the operation. The thread used to continue the operation has all of the thread static values from the last operation using it. Locks do not fix this problem.

Get widget