Thread.CurrentThread.Name を呼び出してスレッド名を取得できることはわかっています。
しかし、私はトリッキーなシナリオを手に入れました。
2 つのスレッドを作成し、それぞれが新しいオブジェクト (objA と言う) を起動し、メソッドを実行します。
オブジェクト (objA) メソッド (objAM) 内で、別のオブジェクト (objB と言います) を作成し、メソッド (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);
}
}
}
ただし、objB の Thread.CurrentThread.Name の値は空を返します。
objBM 内のスレッド名を取得するにはどうすればよいですか?