访问CALL堆栈 [英] Accessing the CALL STACK

查看:65
本文介绍了访问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 the
call stack to ensure it was never within a given method (specifically my own
method). I am hooking into the XmlDocument nodechanged event, I need to from
this event call modify a different node in this xmldocument, thus without
somekind of check we will be off in an infinite loop. Currently I am using
the System.Diagnostics classes but feel its not right to use these classes
like this. I would expect a master call stack to be kepted in the threading
class (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;

}




推荐答案

StackTrace / StackFrame类是您访问调用的方式

堆栈信息。真的没有别的办法了。


但是,你不应该这样做。获得调用堆栈是非常昂贵的,更不用说你应该实现更好的逻辑来确定你是否重新输入自己的函数。

确定你是否重新输入自己的函数。


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- < a href =mailto:mv*@spam.guard.caspershouse.com> mv*@spam.guard.caspershouse.com

" Yahoo" <乔*************** @ yahoo.com>在消息中写道

news:Uz **************** @ newssvr12.news.prodigy.com ...
The StackTrace/StackFrame class is the way that you would access call
stack information. There really isn''t another way.

However, you shouldn''t be doing this. Getting the call stack is
expensive, not to mention the fact that you should implement better logic to
determine if you are re-entering your own function.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Yahoo" <jo***************@yahoo.com> wrote in message
news:Uz****************@newssvr12.news.prodigy.com ...
I有一个编程问题,我需要知道
调用堆栈的整个历史记录,以确保它永远不会在给定的方法中(特别是我自己的方法)。我挂钩进入XmlDocument的nodechanged事件,我需要从这个事件调用来修改这个xmldocument中的一个不同的节点,因此没有一些检查我们将在无限循环中关闭。目前我正在使用System.Diagnostics类但感觉不适合使用这样的这些类。我希望在
线程类(甚至反射)中保持一个主调用堆栈,但我没有看到它。有更好的方法吗?

私人布尔IsFromUs()



StackTrace stackTrace = new StackTrace();

StackFrame stackFrame;

MethodBase stackFrameMethod;

字符串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"))





中断;

}


//现在确定我们是否是这个通知的原因

for(int x = position; x< stackTrace.FrameCount; x ++)

{/>
stackFrame = stackTrace.GetFrame(x);

stackFrameMethod = stackFrame.GetMethod( );

类型Name = stackFrameMethod.ReflectedType.FullName;

if(typeName.StartsWith(" RuleEngine"))

返回true;

}

返回false;

}

I have a programming issue where I need to know the whole history of the
call stack to ensure it was never within a given method (specifically my
own method). I am hooking into the XmlDocument nodechanged event, I need to
from this event call modify a different node in this xmldocument, thus
without somekind of check we will be off in an infinite loop. Currently I
am using the System.Diagnostics classes but feel its not right to use these
classes like this. I would expect a master call stack to be kepted in the
threading class (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;

}




I知道它真正的性能打击,但现在还可以。在此之前,我实现了一个balking设计模式,我正在检查

nodechanged上的状态,但这导致了其他问题。感谢您快速回复



Nicholas Paldino [.NET / C#MVP]" < mv*@spam.guard.caspershouse.com>写在

消息新闻:美国************* @ TK2MSFTNGP10.phx.gbl ...
I know its a real performance hit, but thats ok for now. Before this i was
implementing a balking design pattern where I was cheking the status on the
nodechanged but that was causing other problems. Thanks for the quick
response.
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:uS*************@TK2MSFTNGP10.phx.gbl...
StackTrace / StackFrame类是您将访问的方式调用堆栈信息。真的没有别的办法了。

然而,你不应该这样做。获得调用堆栈是昂贵的,更不用说你应该实现更好的逻辑
来确定你是否重新进入自己的功能。

希望这会有所帮助。

-
- Nicholas Paldino [.NET / C#MVP]
- mv * @ spam.guard.caspershouse.com

雅虎 <乔*************** @ yahoo.com>在消息中写道
新闻:Uz **************** @ newssvr12.news.prodigy.com ...
The StackTrace/StackFrame class is the way that you would access call
stack information. There really isn''t another way.

However, you shouldn''t be doing this. Getting the call stack is
expensive, not to mention the fact that you should implement better logic
to determine if you are re-entering your own function.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Yahoo" <jo***************@yahoo.com> wrote in message
news:Uz****************@newssvr12.news.prodigy.com ...
我有一个编程问题我需要知道
调用堆栈的整个历史记录,以确保它永远不会在给定的方法中(特别是我自己的方法)。我挂钩进入XmlDocument nodechanged事件,我需要从这个事件调用来修改这个xmldocument中的一个不同的节点,因此如果没有某些检查我们将在无限循环中关闭。目前我正在使用System.Diagnostics类但感觉不适合使用
这样的类。我希望在线程类(甚至是反射)中保持一个主调用堆栈,但是我没有看到它。有更好的方法吗?

私人布尔IsFromUs()



StackTrace stackTrace = new StackTrace();

StackFrame stackFrame;

MethodBase stackFrameMethod;

字符串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"))





中断;

}


//现在确定我们是否是这个通知的原因

for(int x = position; x< stackTrace.FrameCount; x ++)

{/>
stackFrame = stackTrace.GetFrame(x);

stackFrameMethod = stackFrame.GetMethod( );

类型Name = stackFrameMethod.ReflectedType.FullName;

if(typeName.StartsWith(" RuleEngine"))

返回true;

}

返回false;

}

I have a programming issue where I need to know the whole history of the
call stack to ensure it was never within a given method (specifically my
own method). I am hooking into the XmlDocument nodechanged event, I need
to from this event call modify a different node in this xmldocument, thus
without somekind of check we will be off in an infinite loop. Currently I
am using the System.Diagnostics classes but feel its not right to use
these classes like this. I would expect a master call stack to be kepted
in the threading class (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;

}





2005年10月27日星期四17:59:16 GMT,Yahoo

< jo *************** @ yahoo.com>写道:
On Thu, 27 Oct 2005 17:59:16 GMT, "Yahoo"
<jo***************@yahoo.com> wrote:
我有一个编程问题,我需要知道
调用堆栈的整个历史,以确保它永远不会在给定的方法中(特别是我自己的
法)。我挂钩进入XmlDocument nodechanged事件,我需要从这个事件调用中修改这个xmldocument中的一个不同的节点,因此没有某些检查我们将在无限循环中关闭。目前我正在使用System.Diagnostics类但感觉不适合使用这样的类
。我希望在线程
类(甚​​至反射)中保持一个主调用堆栈,但我没有看到它。有更好的方法吗?
I have a programming issue where I need to know the whole history of the
call stack to ensure it was never within a given method (specifically my own
method). I am hooking into the XmlDocument nodechanged event, I need to from
this event call modify a different node in this xmldocument, thus without
somekind of check we will be off in an infinite loop. Currently I am using
the System.Diagnostics classes but feel its not right to use these classes
like this. I would expect a master call stack to be kepted in the threading
class (or even reflections) but i dont see it. Is there a better way?




以下代码不会工作:


//每个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堆栈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆