UnsafeNativeMethods.PostMessage-Transition to unmanaged code

Code profiling for .NET Developers

UnsafeNativeMethods.PostMessage-Transition to unmanaged code

Postby setter » Tue Nov 08, 2011 5:59 pm

I'm trying to profile a section of the code that ANTS says %5 of CPU time.

It's reported as

UnsafeNativeMethods.PostMessage > Transition to unmanaged code

In the call graph, all the callers using a pattern like this:

Code: Select all
if (SomethingHappenedHandler != null)

However mostly SomethingHappenedHandler is null.

Unfortunately profiler doesn't tell me what line is about "Transition to unmanaged code" so I'm guess this is the line as all 3 callers got something like this. Another code that might be related is this:

Code: Select all
Interlocked.Increment(ref _DCount);

Most of these events later on handled in the GUI and then GUI uses InvokeRequired and BeginInvokes something.

How can I understand which code leads to "Transition to unmanaged code"? And what does it actually mean?

Another similar one exactly same but this time original method is "SetNativeMethods.GetWindowThreadProcessId" again it's linked to "Transition to unmanaged code" and I can't find which calls actually calls it as there is no line indicators.

Posts: 3
Joined: Tue Nov 08, 2011 5:50 pm

Postby setter » Wed Nov 09, 2011 11:32 am

OK, I figured that "Transition to unmanaged code" is referring to marshalling in UI thread due to invokes.

Still it's not possible to spot this from the source code as profiler doesn't mark it up or show any time allocation on that line, so it's a bit of guess work.
Posts: 3
Joined: Tue Nov 08, 2011 5:50 pm

Postby Brian Donahue » Wed Nov 09, 2011 1:28 pm


You're only going to see "transition to managed code" on the tree view as it's probably going to be difficult to wedge it into the source code view. However, the managed method name invoking the unmanaged code should be directly below the "transition" entry in the tree view. For instance:
Code: Select all
Microsoft.Samples.Debugging.CorDebug.CorDebuggerVersion.GetDebuggerVersionFromPid(int pid)
-> Transition to unmanaged code...
     ->Microsoft.Samples.Debugging.CorDebug.NativeMethods.GetVersionFromProcess(ProcessSafeHandle hProcess, StringBuilder versionString, int bufferSize, out int dwLength)

Where GetVersionFromProcess is a pInvoke defined as...
Code: Select all
[DllImport("mscoree.dll", CharSet=CharSet.Unicode, PreserveSig=true)]
        public static extern void GetVersionFromProcess(ProcessSafeHandle hProcess, StringBuilder versionString,
                                                        Int32 bufferSize, out Int32 dwLength);

I think the "time with children" on the "transition" is not very useful - it seems to include the run-time of the unmanaged methods, too. The "time %" seems more sane (in my case <0.001).

I'm told that if computing resources are low this may affect the reported time in transition. So you may want to turn down the detail level (for instance do not profile with line-level timings on).
Brian Donahue
Posts: 6590
Joined: Mon Aug 23, 2004 9:48 am

Return to ANTS Performance Profiler Previous Versions

Who is online

Users browsing this forum: No registered users and 0 guests