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);
}

You May Also Like

About the Author: rnowik

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.