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;
} 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;
}
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;
}
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
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
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
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
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
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;
}
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;
}
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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) {
|
by: Vinodh Kumar P |
last post by:
What is call stack?
I am sorry if its perceived as an off topic.
|
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...
|
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...
|
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()...
| |
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...
|
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...
|
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.
|
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
|
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...
|
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...
| |
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. ...
|
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...
|
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...
|
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...
|
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...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |