473,320 Members | 2,006 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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.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;

}




Nov 17 '05 #1
11 3863
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 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;

}



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.com> wrote in
message news:uS*************@TK2MSFTNGP10.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.com

"Yahoo" <jo***************@yahoo.com> wrote in message
news:Uz****************@newssvr12.news.prodigy.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.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;

}




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.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?


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.com...
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.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?


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
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.


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.com...
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
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.


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.com>
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.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;

}




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

client->myobjmethod1->myobjectmethod2

or they could

client->myobjectmethod2

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*******@discussions.microsoft.com> wrote in message
news:3B**********************************@microsof t.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.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;

}




Nov 17 '05 #10
I didnt miss the threadstatic att, the problem can be caused when...

client->myobj1

now the ismethodactive for this thread is true. next...

client-> xmldocument->myobj1->myobj2

they should have access to myobj2 but since its of the same type and has a
static (per thread) they will be denied. Heres the problem... its ok if my
objects calls themself (they in the know), but i when myobj1 updates the
xmldocument (or even does the selectsinglenode causing a createnavigator
object to be created) the nodechange event is fired and we end up in with a
stack overflow exception

client->xmldocument->myobj1->xmldocument->myobj1->xmldocuemnt->.....->StackOverflow
Exception

I am thinking that I have a big fundamental design issue?

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
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.com>
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 #11
Yahoo <jo***************@yahoo.com> wrote:
I didnt miss the threadstatic att
I think I misunderstood your comment then.
the problem can be caused when...

client->myobj1

now the ismethodactive for this thread is true. next...

client-> xmldocument->myobj1->myobj2

they should have access to myobj2 but since its of the same type and has a
static (per thread) they will be denied. Heres the problem... its ok if my
objects calls themself (they in the know), but i when myobj1 updates the
xmldocument (or even does the selectsinglenode causing a createnavigator
object to be created) the nodechange event is fired and we end up in with a
stack overflow exception

client->xmldocument->myobj1->xmldocument->myobj1->xmldocuemnt->.....->StackOverflow
Exception

I am thinking that I have a big fundamental design issue?


Well, the ThreadStatic hashtable idea is probably the best one.
(Alternatively, each object could have a hashtable of threads which
were currently "inside" the method.)

--
Jon Skeet - <sk***@pobox.com>
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 #12

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

Similar topics

1
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
by: Vinodh Kumar P | last post by:
What is call stack? I am sorry if its perceived as an off topic.
2
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...
1
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...
3
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...
5
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...
24
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...
6
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...
1
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shćllîpôpď 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.