Debugging with Async in C#

No.of Views609
Bookmarked0 times
Downloads 
Votes0
By  abhi2434   On  25 Nov 2010 07:11:48
Tag : CSharp , General
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
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

 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.

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. 

Image Loading

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()
    {try{

            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 : 

Image Loading

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. 

Image Loading

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

Summary

Through this article,i try to give few information about Async Debuggin in C#.hope this helps.

 
Sign Up to vote for this article
 
About Author
 
abhi2434
Occupation-Not Provided
Company-Not Provided
Member Type-Senior
Location-Not Provided
Joined date-22 Oct 2009
Home Page-Not Provided
Blog Page-Not Provided
 
 
Other popularSectionarticles
    Are you somewhat confused between Serialization and Marshaling? This writing would break this confusion up, it would give you a basic understanding of the process of Serialization and the process of Marshaling, and how you can get the most out of each.
    Published Date : 10/May/2010
    First, this writing concentrates of and compares between three programming languages, C#, C++/CLI, and ISO/ANSI C++. It discusses 9 rules that every developer should keep in mind while working with constructors, destructors, and finalizers and class hierarchies:
    Published Date : 05/May/2010
    This article explains how to filter rows in a DataSet/DataTable. The example provided will help you get information faster.
    Published Date : 23/May/2010
    In this article, I will explain three basic terms of C#, such as Call Stack ,Call Site and stack Unwinding
    Published Date : 17/Aug/2010
    In this article, I will discuss about Checked and unchecked keyword and conversions in C#.
    Published Date : 16/Aug/2010
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top