Reputation: 85
i know i can get the Thread Name by calling Thread.CurrentThread.Name
but i got a tricky scenario.
i created two thread, each launch a new object (says objA) and run a method.
inside the object (objA) method (objAM), i create another object (says objB) and run a method (objBM).
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
TESTA a = new TESTA();
}
}
class TESTA
{
private Thread t;
public TESTA()
{
t = new Thread(StartThread);
t.Name = "ABC";
t.IsBackground = true;
t.Start();
t = new Thread(StartThread);
t.Name = "XYZ";
t.IsBackground = true;
t.Start();
}
private void StartThread()
{
objA thisA = new objA();
}
}
class objA
{
private System.Threading.Timer t1;
public objA()
{
objAM();
t1 = new Timer(new TimerCallback(testthread), null, 0, 1000);
}
private void objAM()
{
Console.WriteLine("ObjA:" + Thread.CurrentThread.Name);
}
private void testthread(object obj)
{
objB thisB = new objB();
}
}
class objB
{
public objB()
{
objBM();
}
private void objBM()
{
Console.WriteLine("ObjB:" + Thread.CurrentThread.Name);
}
}
}
but the value of Thread.CurrentThread.Name in objB return empty.
How can i get the Thread Name inside objBM?
Upvotes: 2
Views: 1742
Reputation: 236208
From description of System.Threading.Timer: The method does not execute on the thread that created the timer; it executes on a ThreadPool thread supplied by the system.
Thus your testthread
method executed on unnamed ThreadPool thread. Btw you can verify it by calling Thread.CurrentThread.IsThreadPoolThread
.
Upvotes: 3