本文介绍了访问CALL堆栈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有一个编程问题,我需要知道 调用堆栈的整个历史记录,以确保它永远不会在给定的方法中(特别是我自己的 方法) )。我挂钩进入XmlDocument nodechanged事件,我需要从 这个事件调用修改这个xmldocument中的一个不同的节点,因此没有 一些检查我们将关闭无限循环。目前我正在使用 的System.Diagnostics类但感觉不适合使用这些类 这样。我希望在线程 类(甚至反射)中保持一个主调用堆栈,但我没有看到它。有没有更好的方法? 私人布尔IsFromUs() { StackTrace stackTrace = new StackTrace( ); StackFrame stackFrame; MethodBase stackFrameMethod; string typeName; //找到第一个system.xml方法,这总是存在 int position = 0; for(int x = position; x< stackTrace.FrameCount; x ++) { stackFrame = stackTrace.GetFrame(x); stackFrameMethod = stackFrame.GetMethod(); typeName = stackFrameMethod.ReflectedType.FullName; if( typeName.StartsWith(" System.Xml")) { position = x; 休息; } } //现在确定我们是不是这个通知的原因 for(int x = position; x< stackTrace.FrameCount; x ++) { stackFrame = stackTrace.GetFrame(x); stackFrameMethod = stackFrame.GetMethod(); typeName = stackFrameMethod.ReflectedType.FullName; if(typeName.StartsWith(" RuleEngine")) 返回true; } 返回false; } /> I have a programming issue where I need to know the whole history of thecall stack to ensure it was never within a given method (specifically my ownmethod). I am hooking into the XmlDocument nodechanged event, I need to fromthis event call modify a different node in this xmldocument, thus withoutsomekind of check we will be off in an infinite loop. Currently I am usingthe System.Diagnostics classes but feel its not right to use these classeslike this. I would expect a master call stack to be kepted in the threadingclass (or even reflections) but i dont see it. Is there a better way?private bool IsFromUs() { StackTrace stackTrace = new StackTrace(); StackFrame stackFrame; MethodBase stackFrameMethod; string typeName; //find the first system.xml method, this always exists int position=0; for(int x = position; x<stackTrace.FrameCount; x++) { stackFrame = stackTrace.GetFrame(x); stackFrameMethod = stackFrame.GetMethod(); typeName = stackFrameMethod.ReflectedType.FullName;if (typeName.StartsWith("System.Xml")) { position = x; break; } } //now determine if we were the cause of this notification for(int x = position; x<stackTrace.FrameCount; x++) { stackFrame = stackTrace.GetFrame(x); stackFrameMethod = stackFrame.GetMethod(); typeName = stackFrameMethod.ReflectedType.FullName;if (typeName.StartsWith("RuleEngine")) return true; } return false; } 推荐答案 以下代码不会工作: //每个thread获取自己的isMethodActive变量。所以这个方法可以同时在几个线程上调用。 [ThreadStatic] 静态bool isMethodActive = false; public void Method() { if(isMethodActive) return; //方法已被调用。退出以避免无限循环。 isMethodActive = true; 尝试 { / *这里递归调用* / } 终于 { isMethodActive = false; } } - MarcusAndrén Wouldn''t the following code work: // Each thread gets its own isMethodActive variable. So the method can// be called on several threads at once.[ThreadStatic]static bool isMethodActive=false; public void Method(){if (isMethodActive)return; //Method is already called. Exit to avoid infinite loop.isMethodActive=true;try{/* Do recursive call here */}finally{isMethodActive=false;}} --Marcus Andrén 这篇关于访问CALL堆栈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-17 15:48