C#: What threads are custom controls running in

Here’s a useful (well maybe) block of code to determine what thread controls are running in.

Note that this code was placed in a base class (hence the 1s on the stack frame to show callers etc).

protected override void CreateChildControls()
{
  var stack = new StackTrace();
  var message = string.Format(
    CultureInfo.InvariantCulture,
    “THREAD ID: {0}, CLASSNAME: {1}, METHOD: {2}”,
    Thread.CurrentThread.ManagedThreadId,
    stack.GetFrame(1).GetMethod().ReflectedType.FullName,
    stack.GetFrame(1).GetMethod().Name);
  Debug.WriteLine(message);
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>