简体   繁体   English

如何判断我的代码是否已从立即窗口调用?

[英]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. 当使用该库的发行版本,但调试已加载包含该库的DLL的应用程序时,如果我从立即窗口(x.prop = true)更改属性,我希望我的属性代码检测到并执行不同。

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. 要检查的唯一一件事,如果来自立即窗口的调用是创建一个线程的函数的OUTSIDE。 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. 我希望,这是可以理解的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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