.NET Framework Bookmark and Share   
 index > Building Development and Diagnostic Tools for .Net > Notification when managed thread exits
 

Notification when managed thread exits

The first managed frame on a thread is usually 'Main'. Can I be notified when this frame 'steps out'? I have tried to create stepper on that frame, but it does not seem to work.

I need this for two things:

  1. I would like to 'inject' a call to { System.Console.WriteLine(“Press any Enter to continue...�; System.Console.ReadLine(); } so that when a console application exits, I can still see the output.

  2. Application is kept alive because of worker threads. When the frame 'Main' steps out, ExitThread callback does not occur because native thread is still running. However, I want to remove the thread from my data structures anyway so that I do not attempt to access it.

David Srbecky

I'm not sure offhand how to do this.

I don't think ICorDebug will help you here. Some issues:
1. Step-out won't solve it. Steppers only complete in managed code, and so you can't "step out" of main to detect its exit. (In theory, you could write an interop-debugging to step out of main to the unmanaged caller, but that would be crazy here.)
2. ThreadExits come later than just when there's no managed code on the stack (http://blogs.msdn.com/jmstall/archive/2006/02/01/icordebugthread-trivia.aspx)
There may even be goofy scenarios where them main thread executes managed code after main has returned. (I'm not 100% sure here, but it seems like something that could be arranged).

Possible options:
1. Launch the debuggee under a host which invokes main and then prints out the message when main returns.
2. Perhaps you can play some games with overriding stdin/stdout. Not exactly sure what though.

Mike Stall - MSFT

I'm not sure offhand how to do this.

I don't think ICorDebug will help you here. Some issues:
1. Step-out won't solve it. Steppers only complete in managed code, and so you can't "step out" of main to detect its exit. (In theory, you could write an interop-debugging to step out of main to the unmanaged caller, but that would be crazy here.)
2. ThreadExits come later than just when there's no managed code on the stack (http://blogs.msdn.com/jmstall/archive/2006/02/01/icordebugthread-trivia.aspx)
There may even be goofy scenarios where them main thread executes managed code after main has returned. (I'm not 100% sure here, but it seems like something that could be arranged).

Possible options:
1. Launch the debuggee under a host which invokes main and then prints out the message when main returns.
2. Perhaps you can play some games with overriding stdin/stdout. Not exactly sure what though.

Mike Stall - MSFT

You can use google to search for other answers

Custom Search

More Threads

• IronPython output is missing
• Re: How can I tell if I am being called from funceval
• Profiler activation for services and ASP.NET?
• ngen and Enter leave tail hooks
• ICorProfilerInfo::GetILFunctionBody doesn't return the correct size on methods with SEH?
• How to call C# generic method in emitted IL
• Debugging modified IL
• A tool for finding what .NET Assembly versions a DLL is dependent on ?
• GetDocumentType is there a list matching guids with the type of the document?
• debugger (mdbg lib) vs profiler ...