473,769 Members | 2,643 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Get the name of the calling function of an executing function

Hi, I want to get the name of the calling function of an executing function,
I use the StackTrace class to do this and it seems working. However, does
anyone think that there any side effect towards this approach such as how
would it works in multi-thread.

Thanks
Tony


// call the HI method from somewhere.

private void HI(){
System.Diagnost ics.StackTrace st = new System.Diagnost ics.StackTrace( 1);
System.Diagnost ics.StackFrame sf = st.GetFrame(0);
string msg = sf.GetMethod(). DeclaringType.F ullName + "." +
sf.GetMethod(). Name;
MessageBox.Show ( msg );
}
Nov 13 '05 #1
2 9781
Tony Liu <en*******@hotm ail.com> wrote:
Hi, I want to get the name of the calling function of an executing function,
I use the StackTrace class to do this and it seems working. However, does
anyone think that there any side effect towards this approach such as how
would it works in multi-thread.


Multithreading shouldn't have any effect. Things which might:

o JIT inlining could remove some stack frames
o This method may well slow things down considerably - don't try to use
it in intensive code

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 13 '05 #2
Hi Tony,

Since each thread has its own stack, StackTrace will work in multiple
threads just as it does in single thread. Here I have a demo, you may
create a new C# Windows application project, and modify Form1.cs as follows.
using System;
using System.Drawing;
using System.Collecti ons;
using System.Componen tModel;
using System.Windows. Forms;
using System.Data;
using System.Threadin g;
using System.Diagnost ics;
namespace test1
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows. Forms.Form
{
private System.Windows. Forms.Button button1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.Componen tModel.Containe r components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeCompo nent();
//
// TODO: Add any constructor code after
InitializeCompo nent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Disp ose();
}
}
base.Dispose( disposing );
}
private void HI()
{
System.Diagnost ics.StackTrace st = new
System.Diagnost ics.StackTrace( 1);
System.Diagnost ics.StackFrame sf = st.GetFrame(0);
string msg = sf.GetMethod(). DeclaringType.F ullName
+ "." +
sf.GetMethod(). Name;
MessageBox.Show (msg);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
{
this.button1 = new System.Windows. Forms.Button();
this.SuspendLay out();
//
// button1
//
this.button1.Lo cation = new
System.Drawing. Point(80, 216);
this.button1.Na me = "button1";
this.button1.Ta bIndex = 0;
this.button1.Te xt = "button1";
this.button1.Cl ick += new
System.EventHan dler(this.butto n1_Click);
//
// Form1
//
this.AutoScaleB aseSize = new System.Drawing. Size(5,
13);
this.ClientSize = new System.Drawing. Size(292,
273);
this.Controls.A dd(this.button1 );
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayo ut(false);
}
#endregion
public void test()
{
HI();
}
public void test2()
{
HI();
}
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
Application.Run (new Form1());
}
private void button1_Click(o bject sender, System.EventArg s
e)
{
Thread InstanceCaller = new Thread(new
ThreadStart(thi s.test));
// Start the thread.
InstanceCaller. Start();
Thread StaticCaller = new Thread(new
ThreadStart(thi s.test2));
// Start the thread.
StaticCaller.St art();

}
}
}
Hope this will help you.
--------------------
From: Jon Skeet <sk***@pobox.co m>
Subject: Re: Get the name of the calling function of an executing function
Date: Wed, 9 Jul 2003 09:58:35 +0100
Message-ID: <MP************ ************@ne ws.microsoft.co m>
References: <ez************ **@TK2MSFTNGP11 .phx.gbl>
Organization : Peramon Technology Ltd.
X-Newsreader: MicroPlanet Gravity v2.60
Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
NNTP-Posting-Host: fw.peramon.com 193.132.195.125
Lines: 1
Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!tk2 msftngp13.phx.g bl
Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:1678 58
X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp

Tony Liu <en*******@hotm ail.com> wrote:
Hi, I want to get the name of the calling function of an executing function, I use the StackTrace class to do this and it seems working. However, does anyone think that there any side effect towards this approach such as how
would it works in multi-thread.


Multithreadi ng shouldn't have any effect. Things which might:

o JIT inlining could remove some stack frames
o This method may well slow things down considerably - don't try to use
it in intensive code

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too


Nov 13 '05 #3

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

Similar topics

3
2415
by: Andrew Boothman | last post by:
Hi, This may be the dumbest question ever, but in the following code (using PHP 4.3.8) how do I call b() from within a()? class test { function a() { print "a called"; b();
6
4066
by: vishal | last post by:
hi how can i get the file name from which the current executing file is called. i m making a scrript which will be called from many other scripts. this script performs some calculation and then returns the result to the script who asked for. so i want to know the script name of caller script in the current executing script thxs for your help in advance.
33
2535
by: Jacek Generowicz | last post by:
I would like to write a metaclass which would allow me to overload names in the definition of its instances, like this class Foo(object): __metaclass__ = OverloadingClass att = 1 att = 3
8
10550
by: Evan | last post by:
Hi I have a short script that makes 2 calls to methods in another script as follows: import canPlaces as canp callOne=canp.addMe(3,5) callTwo=canp.estocStn()
15
22861
by: dspfun | last post by:
Hi, Is it possible to print the function name of the calling function? For example, f1() and f2() both calls f3(), in f3() I would like to print the name of the function calling f3() which could either be f1() or f2(). BRs!
9
26280
by: | last post by:
Is it possible to get the name of the method that the code is currently executing in so that I can pass it to another method? For example, in void method1() I will call void method5(). In method5 I want to know which method called me? Because there are many methods calling many other methods I really want a trace of code execution from method to method. Know what I mean? any advice would be greatly appreciated.
1
8556
by: newbie | last post by:
This is a snippet from C++ FAQs, which I have never done--- when I do such a thing, I would declare function used by constructor ( in this example, init() ) as static. But I do understand that it would be great if the following practice is valid. But by OO concept, it seems not to very good because init() belongs to an object, which hasn't be constructed. Can you give any comment on my question?
9
2100
by: kj7ny | last post by:
Is there a way that I can programmatically find the name of a method I have created from within that method? I would like to be able to log a message from within that method (def) and I would like to include the name of the method from which it was written without having to hard-code that value in every message string. While we're at it, is there a way to programmatically get the name of the class and the module while I'm at it? ...
5
2079
by: vsachar | last post by:
When executing a C++ program, How can I find out the name of the function that is currently being executed. If assume my program main is calling a function f1() and that in turn is calling f2(). Then at any given time how can I find out which function has completed execution. i am creating an automatic garbage collector and I need to know where the program control is during collection phase so that the memory of the functions that have finished...
0
9589
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10047
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9995
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9863
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8872
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6674
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3962
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2815
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.