Creo que tu pregunta se puede interpretar de varias maneras, y de si intentas obtenerlo el nombre del método desde el thread adicional o el thread de la UI, por favor, muestra el código relevante.
De todas formas, creo que este ejemplo basado en
Reflection solventará tus dudas:
Vb.Net:
Imports System.Reflection
Imports System.Threading.Tasks
Public NotInheritable Class Main : Inherits Form
Private Sub Test() Handles MyBase.Shown
Dim t As New Thread(AddressOf Method1)
t.Start()
Debug.
WriteLine("Main: " & MethodBase.
GetCurrentMethod().
Name) ' Test
Task.Factory.StartNew(
Sub()
Debug.
WriteLine("Lambda: " & MethodBase.
GetCurrentMethod().
Name) ' _Lambda$__n End Sub)
End Sub
Private Sub Method1()
Dim stackTrace As New StackTrace
Debug.
WriteLine("Thread: " & MethodBase.
GetCurrentMethod().
Name) ' Method1
End Sub
End Class
Traducción online a C#:
using System.Diagnostics;
using System.Reflection;
using System.Threading.Tasks;
public sealed class Main : Form {
public Form1() {
Shown += Test;
}
private void Test() {
Thread t
= new Thread
(Method1
); t.Start();
Debug.WriteLine("Main: " + MethodBase.GetCurrentMethod().Name); // Test
Task.Factory.StartNew(() => { Debug.WriteLine("Lambda: " + MethodBase.GetCurrentMethod().Name); }); // _Lambda$__n
}
private void Method1() {
StackTrace stackTrace
= new StackTrace
(); Debug.WriteLine("Thread: " + MethodBase.GetCurrentMethod().Name); // Method1
}
}
//=======================================================
//Service provided by Telerik (www.telerik.com)
//=======================================================
Resultado de ejecución:
Main: Test
Thread: Method1
Lambda: _Lambda$__2
Si lo prefieres también puedes utilizar una propiedad como esta de aquí abajo para tenerla siempre a mano.
El código fuente lo he extraido de mi API
ElektroKit:
Vb.Net:
''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' Gets the current executing member in the stack trace of the application.
''' </summary>
''' ----------------------------------------------------------------------------------------------------
''' <example> This is a code example.
''' <code>
''' Private Sub TestMethod()
'''
''' MsgBox(CurrentMember.Name)
'''
''' End Sub
''' </code>
''' </example>
''' ----------------------------------------------------------------------------------------------------
''' <value>
''' The current executing member.
''' </value>
''' ----------------------------------------------------------------------------------------------------
Public Shared ReadOnly Property CurrentMember As MethodBase
<DebuggerStepThrough>
Get
Dim stackTrace As New StackTrace
Return stackTrace.GetFrame(1).GetMethod()
End Get
End Property
Traducción online a C#:
/// ----------------------------------------------------------------------------------------------------
/// <summary>
/// Gets the current executing member in the stack trace of the application.
/// </summary>
/// ----------------------------------------------------------------------------------------------------
/// <value>
/// The current executing member.
/// </value>
/// ----------------------------------------------------------------------------------------------------
public static MethodBase CurrentMember {
get {
StackTrace stackTrace
= new StackTrace
(); return stackTrace.GetFrame(1).GetMethod();
}
}
//=======================================================
//Service provided by Telerik (www.telerik.com)
//=======================================================
Modo de empleo:
Vb.Net:
Imports System.Reflection
Imports System.Threading.Tasks
Public NotInheritable Class Main : Inherits Form
Private Sub Test() Handles MyBase.Shown
Dim t As New Thread(AddressOf Method1)
t.Start()
Debug.
WriteLine("Main: " & CurrentMember.
Name) ' Test
Task.Factory.StartNew(
Sub()
Debug.
WriteLine("Lambda: " & CurrentMember.
Name) ' _Lambda$__n End Sub)
End Sub
Private Sub Method1()
Dim stackTrace As New StackTrace
Debug.
WriteLine("Thread: " & CurrentMember.
Name) ' Method1
End Sub
Public Shared ReadOnly Property CurrentMember As MethodBase
<DebuggerStepThrough>
Get
Dim stackTrace As New StackTrace
Return stackTrace.GetFrame(1).GetMethod()
End Get
End Property
End Class
C#:
using System.Diagnostics;
using System.Reflection;
using System.Threading.Tasks;
public sealed class Main : Form {
public Form1() {
Shown += Test;
}
private void Test() {
Thread t
= new Thread
(Method1
); t.Start();
Debug.WriteLine("Main: " + CurrentMember.Name); // Test
Task.Factory.StartNew(() => { Debug.WriteLine("Lambda: " + CurrentMember.Name); }); // _Lambda$__n
}
private void Method1() {
StackTrace stackTrace
= new StackTrace
(); Debug.WriteLine("Thread: " + CurrentMember.Name); // Method1
}
public static MethodBase CurrentMember {
[DebuggerStepThrough()]
get {
StackTrace stackTrace
= new StackTrace
(); return stackTrace.GetFrame(1).GetMethod();
}
}
}
//=======================================================
//Service provided by Telerik (www.telerik.com)
//=======================================================
Resultado de ejecución:
Main: Test
Thread: Method1
Lambda: _Lambda$__2
Saludos!