I'd like to get the running function's name every second. I tried with this code:
public static void DisplayTimeEvent(object source, ElapsedEventArgs e)
{
// code here will run every second
//Output is DisplayTimeEvent but I want it to print Main
Console.WriteLine(GetCurrentMethod());
}
[MethodImpl(MethodImplOptions.NoInlining)]
public static string GetCurrentMethod()
{
StackTrace st = new StackTrace();
StackFrame sf = st.GetFrame(1);
return sf.GetMethod().Name;
}
//main function
Timer myTimer = new Timer();
myTimer.Elapsed += new ElapsedEventHandler(DisplayTimeEvent);
myTimer.Interval = 1000;
myTimer.Start();
Console.ReadLine();
The code above prints DisplayTimeEvent
. How can I make it print Main (or whatever function main thread is executing at that moment)?