Debugging with Async

C# async feature really exposes a lots of things to us. I can see, there is lots of people is discussing about it in MSDN forums, few people wanted to get rid of the Task from the async methods and really want to deal with normal return types, while others just wanted to get rid of the async postfix. During my leisure, I read them but what I thought the most important part of the feedback is that most of the people really liked the way Microsoft simplifies the Asynchronous approach of programming style. 

I have already studied some of the important facts on Async style of programming, and found that it uses the StateMachine to let the program return after a certain portion of the method gets executed while the Rest of the program is sent as ContinueWith for the Task. Thus the TaskCompletion automatically restarts the method and continue. If you want to know more about how Task gets executed in async methods, please read my article where I have discussed how exactly the current trend of programming style got changed.

Today while I was looking at the implementation of Async style of programming, I just discovered an Internal class named DebugInfo inside the System.Runtime.CompilerServices.AsyncMethodBuilder.

You can see the class is internal, and hence not exposed from the perspective of BCL. You cannot create object of the class or even look at the private member LocationForDebuggerDisplay. But the only thing that you can see through this is the ActiveMethods.

The ActiveMethods lists all the active DebugInfo objects.  To see the object, I have just added few lines to actually load the DebugInfo objects into a dynamic variable.

public static class DebuggerInformation
    public static void WriteDebuggerInfo()

            Type typ = typeof(System.Runtime.CompilerServices.AsyncMethodBuilder);
            var info = typ.GetNestedType("DebugInfo", BindingFlags.Static | BindingFlags.NonPublic);
            var pobj = info.GetProperty("ActiveMethods", BindingFlags.Static | BindingFlags.NonPublic);

            var obj = pobj.GetValue(null, null) as IList;
            foreach (dynamic d in obj)
        catch { return; }

So eventually, this let me to grab the objects(Task) that were currently running in the context.

Now while I run my application, and also in a mode when few methods are waiting to be finished, you can put a breakpoint to the program and invoke the debugger. Let me do this with my existing application and see what it lists :

In the figure you can see, it lists most of the important features like StartTime, TaskStatus, StratingStackTrace, Thread in which it runs etc. Thus these objects could be used for debugging purpose in the current CTP build of Async framework.

The DebugInfo object is created when the AsyncMethodBuilder creates a TaskCompletitionSource object while creating the StateMachine. If you see the AsyncMethodBuilder you will see it actually puts the DebugInfo object into the ConcurrentDictionary with Task created.

But this is in very preliminary stage, I am hoping there will be much more things to display when later builds are available.

Thank you for reading.
Shout it Submit this story to DotNetKicks Bookmark and Share
Read Disclaimer Notice


Post a Comment

Please make sure that the question you ask is somehow related to the post you choose. Otherwise you post your general question in Forum section.

Author's new book

Abhishek authored one of the best selling book of .NET. It covers ASP.NET, WPF, Windows 8, Threading, Memory Management, Internals, Visual Studio, HTML5, JQuery and many more...
Grab it now !!!