Hi,

Due to modern application design it is important to work as parallel as you cankinda divide and conquer (and merge).
In order to followthe paradigma of parallel programming, many callsare executed asynchronous -but then later reading the application trace will be muchharder.

So my question:
Is there a simple way toinformation from the .Net Frameworkfor the questionwho is thecallerofthe currently executed asynchronous call.What wouldbe the bestapproach to get this information (IL rewriting ? ), any ideas ?

Thank you,
Holger

Answers

  • Wednesday, May 27, 2009 12:00 AMp.b.a Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    There are a variety of ways to start asynchronous jobs - calling a delegate's BeginInvoke, using ThreadPool.QueueUserWorkItem or using System.Threading.Tasks - but they all (AFAIK) take a delegate callback as a parameter.

    Using IL rewriting you could add a new field to the delegate which will store the async operation initiation stack trace so all you need to do next is automatically populate that field at runtime. Ideally you would rewrite the methods that schedule async operations (QueueUserWorkItem, task factory's StartNew) but that is not always possible because some of them are generated at runtime - like BeginInvoke methods - and cannot be rewritten. What I would do is rewrite MulticastDelegate's (or Delegate's) constructors to automatically register the call stack, this will at least tell you when the callback delegate was created.

    Another solution would be to register for function enter, monitor for the required methods (QueueUserWorkItem, etc), get the stack right there then expose a service to the managed code to get the stack back. The problem with this approach is resource management, you need to clean up the data for the delegates that are gone - have been garbage collected.

    HTH
    Paul

All Replies

  • Wednesday, May 27, 2009 12:00 AMp.b.a Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    There are a variety of ways to start asynchronous jobs - calling a delegate's BeginInvoke, using ThreadPool.QueueUserWorkItem or using System.Threading.Tasks - but they all (AFAIK) take a delegate callback as a parameter.

    Using IL rewriting you could add a new field to the delegate which will store the async operation initiation stack trace so all you need to do next is automatically populate that field at runtime. Ideally you would rewrite the methods that schedule async operations (QueueUserWorkItem, task factory's StartNew) but that is not always possible because some of them are generated at runtime - like BeginInvoke methods - and cannot be rewritten. What I would do is rewrite MulticastDelegate's (or Delegate's) constructors to automatically register the call stack, this will at least tell you when the callback delegate was created.

    Another solution would be to register for function enter, monitor for the required methods (QueueUserWorkItem, etc), get the stack right there then expose a service to the managed code to get the stack back. The problem with this approach is resource management, you need to clean up the data for the delegates that are gone - have been garbage collected.

    HTH
    Paul
  • Wednesday, May 27, 2009 7:14 AMholger heinrich Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thank you Paul for your detailed, helpfulideas.