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