简体   繁体   中英

How can I tell if my code has been called from the Immediate Window?

Is there any way that my code can tell the difference between "I am executing in normal process context" and "I have been executed from the Immediate Window of the Debugger"?

I have a library object which exposes a public property. When using the release version of that library, but debugging an application that has loaded the DLL containing that library, if I change the property from the Immediate Window (x.prop = true), I would like my property code to detect this and execute differently.

Is there a way to detect this?

For convenience sake, I would also like this to work when I am using the debug version of that library. So, I can't just check to see if there's a debugger attached, and branch from there.

One thing I've always noticed is that any calls from the Immediate Window contain at least one call originating from the following namespace:

Microsoft.VisualStudio.HostingProcess

So if you had, say:

public static string DumpStack()
{
    return new StackTrace().ToString();
}

You'd see something akin to:

   at ConsoleApplication1.Program.DumpStack()
   at ConsoleApplication1.Program.Main(String[] args)
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

the only 1 thing to check, if the call is from the Immediate Window is to create OUTSIDE of your function one thread. That Thread sets a Boolean.

Normally, the immediate window is not able to use other threads, because they are frozen. Means, if your boolean doenst changed, then you are a immediate window.

I hope, that this is understandable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM