473,568 Members | 2,962 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Accessing the CALL STACK

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.Diagnost ics 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 stackFrameMetho d;

string typeName;

//find the first system.xml method, this always exists

int position=0;

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

{

stackFrame = stackTrace.GetF rame(x);

stackFrameMetho d = stackFrame.GetM ethod();

typeName = stackFrameMetho d.ReflectedType .FullName;
if (typeName.Start sWith("System.X ml"))

{

position = x;

break;

}

}

//now determine if we were the cause of this notification

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

{

stackFrame = stackTrace.GetF rame(x);

stackFrameMetho d = stackFrame.GetM ethod();

typeName = stackFrameMetho d.ReflectedType .FullName;
if (typeName.Start sWith("RuleEngi ne"))

return true;

}

return false;

}




Nov 17 '05 #1
11 3882
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.co m

"Yahoo" <jo************ ***@yahoo.com> wrote in message
news:Uz******** ********@newssv r12.news.prodig y.com...
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.Diagnost ics 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 stackFrameMetho d;

string typeName;

//find the first system.xml method, this always exists

int position=0;

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

{

stackFrame = stackTrace.GetF rame(x);

stackFrameMetho d = stackFrame.GetM ethod();

typeName = stackFrameMetho d.ReflectedType .FullName;
if (typeName.Start sWith("System.X ml"))

{

position = x;

break;

}

}

//now determine if we were the cause of this notification

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

{

stackFrame = stackTrace.GetF rame(x);

stackFrameMetho d = stackFrame.GetM ethod();

typeName = stackFrameMetho d.ReflectedType .FullName;
if (typeName.Start sWith("RuleEngi ne"))

return true;

}

return false;

}



Nov 17 '05 #2
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.c om> wrote in
message news:uS******** *****@TK2MSFTNG P10.phx.gbl...
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.co m

"Yahoo" <jo************ ***@yahoo.com> wrote in message
news:Uz******** ********@newssv r12.news.prodig y.com...
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.Diagnost ics 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 stackFrameMetho d;

string typeName;

//find the first system.xml method, this always exists

int position=0;

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

{

stackFrame = stackTrace.GetF rame(x);

stackFrameMetho d = stackFrame.GetM ethod();

typeName = stackFrameMetho d.ReflectedType .FullName;
if (typeName.Start sWith("System.X ml"))

{

position = x;

break;

}

}

//now determine if we were the cause of this notification

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

{

stackFrame = stackTrace.GetF rame(x);

stackFrameMetho d = stackFrame.GetM ethod();

typeName = stackFrameMetho d.ReflectedType .FullName;
if (typeName.Start sWith("RuleEngi ne"))

return true;

}

return false;

}




Nov 17 '05 #3
On Thu, 27 Oct 2005 17:59:16 GMT, "Yahoo"
<jo************ ***@yahoo.com> wrote:
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.Diagnost ics 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?


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
Nov 17 '05 #4
I might be mistaken but i dont thing this would work becuase the
isMethodActive is static, other instances of the object who should be
allowed access would be restricted. it would probably work as an instance
variable with a proxy design pattern where outside calls in the proxy would
set the ismethodactive to true, internal calls that then trigger the
nodechange could then be ignored. I dont think it would be thread safe since
another thread could be denied when it shouldnt be.
"Marcus Andrén" <a@b.c> wrote in message
news:fq******** *************** *********@4ax.c om...
On Thu, 27 Oct 2005 17:59:16 GMT, "Yahoo"
<jo************ ***@yahoo.com> wrote:
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.Diagnost ics 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?


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

Nov 17 '05 #5
On Thu, 27 Oct 2005 19:25:09 GMT, "Yahoo"
<jo************ ***@yahoo.com> wrote:
I might be mistaken but i dont thing this would work becuase the
isMethodActi ve is static, other instances of the object who should be
allowed access would be restricted. it would probably work as an instance
variable with a proxy design pattern where outside calls in the proxy would
set the ismethodactive to true, internal calls that then trigger the
nodechange could then be ignored. I dont think it would be thread safe since
another thread could be denied when it shouldnt be.


True. If you want to allow the method to be called once for each
instance, just remove the static declaration.

If you need it to be threadsafe, replace the static boolean with a
static Hashtable and add/remove 'this'. It is still much better than
trying to look in the stacktrace.

--
Marcus Andrén
Nov 17 '05 #6
Thats hashtable idea sounds good, I will try that. Thanks.

"Marcus Andrén" <a@b.c> wrote in message
news:7m******** *************** *********@4ax.c om...
On Thu, 27 Oct 2005 19:25:09 GMT, "Yahoo"
<jo************ ***@yahoo.com> wrote:
I might be mistaken but i dont thing this would work becuase the
isMethodActiv e is static, other instances of the object who should be
allowed access would be restricted. it would probably work as an instance
variable with a proxy design pattern where outside calls in the proxy
would
set the ismethodactive to true, internal calls that then trigger the
nodechange could then be ignored. I dont think it would be thread safe
since
another thread could be denied when it shouldnt be.


True. If you want to allow the method to be called once for each
instance, just remove the static declaration.

If you need it to be threadsafe, replace the static boolean with a
static Hashtable and add/remove 'this'. It is still much better than
trying to look in the stacktrace.

--
Marcus Andrén

Nov 17 '05 #7
Yahoo <jo************ ***@yahoo.com> wrote:
I might be mistaken but i dont thing this would work becuase the
isMethodActive is static, other instances of the object who should be
allowed access would be restricted.


You missed the fact that it's marked with the ThreadStatic attribute.
That means there's basically one variable per thread, which is exactly
what you want.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #8
in addition to the ThreadStatic idea, you can also dettach from the event on
entering the handler, and re-attach before exit.

"Yahoo" wrote:
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.Diagnost ics 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 stackFrameMetho d;

string typeName;

//find the first system.xml method, this always exists

int position=0;

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

{

stackFrame = stackTrace.GetF rame(x);

stackFrameMetho d = stackFrame.GetM ethod();

typeName = stackFrameMetho d.ReflectedType .FullName;
if (typeName.Start sWith("System.X ml"))

{

position = x;

break;

}

}

//now determine if we were the cause of this notification

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

{

stackFrame = stackTrace.GetF rame(x);

stackFrameMetho d = stackFrame.GetM ethod();

typeName = stackFrameMetho d.ReflectedType .FullName;
if (typeName.Start sWith("RuleEngi ne"))

return true;

}

return false;

}




Nov 17 '05 #9
Tried this and had problems. Issue is the client could...

client->myobjmethod1->myobjectmethod 2

or they could

client->myobjectmethod 2

So in the second method you wouldnt want to add them back in if myobjmethod1
called it cuz that would mess it up for the rest of mymethod1.
"Daniel Jin" <Da*******@disc ussions.microso ft.com> wrote in message
news:3B******** *************** ***********@mic rosoft.com...
in addition to the ThreadStatic idea, you can also dettach from the event
on
entering the handler, and re-attach before exit.

"Yahoo" wrote:
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.Diagnost ics 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 stackFrameMetho d;

string typeName;

//find the first system.xml method, this always exists

int position=0;

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

{

stackFrame = stackTrace.GetF rame(x);

stackFrameMetho d = stackFrame.GetM ethod();

typeName = stackFrameMetho d.ReflectedType .FullName;
if (typeName.Start sWith("System.X ml"))

{

position = x;

break;

}

}

//now determine if we were the cause of this notification

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

{

stackFrame = stackTrace.GetF rame(x);

stackFrameMetho d = stackFrame.GetM ethod();

typeName = stackFrameMetho d.ReflectedType .FullName;
if (typeName.Start sWith("RuleEngi ne"))

return true;

}

return false;

}




Nov 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
10176
by: Marc-Andre Michel | last post by:
Hi, With the following c-like code: void test3(void) { string* s = NULL; cout << s.length(); } void test2(void) {
3
2357
by: Vinodh Kumar P | last post by:
What is call stack? I am sorry if its perceived as an off topic.
2
2517
by: Verane | last post by:
Hi all, I am writing code in C# using visual studio .NET 2003. I use System.Diagnostics.Trace.Assert, and when I get an assertion at run time, I can see the call stack. But for each line of the call stack I don't see the line number, except for the last line of the stack (which corresponds to the Application.run in fact). one precision...
1
4774
by: Jason Coyne | last post by:
I am trying to use the StackTrace class to get my current stack trace for some logging. Everything is working fine, except when I am using threading (specifically WaitCallBack and ThreadPool.QueueUserWorkItem) When I try to walk the stack from a location that has been called via QueueUserWorkItem, the stack stops at the point where the...
3
1527
by: Tommy Vercetti | last post by:
I have a complex threading deadlock scenario that I've been able to reproduce in the debugger. I hit break and look at the call stack which should tell me what I need. Except I only get the very bottom of the call stack: KERNEL32.DLL!7c573b28() KERNEL32.DLL!7c573b50()...
5
1848
by: Bob Day | last post by:
Using vs 2003, vb.net It is not clear from the documentation, but it appears that a raised event will traverse up the call stack upwards until it reaches its handler. Is this correct? See the code snippet below. The event raised in sub1 or class three will be caught in Class1 event handler (I know below is not syntactically correct, but...
24
6559
by: John | last post by:
I know this is a very fundamental question. I am still quite confused if the program call stack stack should always grows upwards from the bottom, or the opposite, or doesn't matter?? That means the stack pointer should go upwards when there are "push" operations, and stack pointer should go downards when there are "pop" operations?? If...
6
1427
by: Tony | last post by:
Is there any value to pursuing program designs that mimimize the mainline call stack? For example, within main() I could code up something like: while(GetMsg(m)) DispatchMsg(m); instead of doing Program.Run() from main() or even worse calling Run() from the Program constructor.
1
7548
by: michael | last post by:
Hi I was wondering if anyone new how to access the call stack information via TSQL I'm currently in the process of migrating an application from Oracle to Sql Server 2005 There is a long running batch process where we currently catch the error and collect the information of the call stack and allow the
0
7693
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7917
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8118
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7665
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5501
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1207
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
933
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.