我有以下代码,我开始Thread
使用ParameterizedThreadStart
对象作为构造函数参数:
static object obj = new object();
static void Main(string[] args)
{
ParameterizedThreadStart start = (o) =>
{
ThreadTest(o);
};
var t = new Thread(() => start(obj));
t.Name = "t";
t.Start();
Thread.Sleep(3000);
obj = null;
// Why the Thread (t) continue here??
Console.ReadKey();
}
private static void ThreadTest(object o)
{
while (o != null)
{
Console.WriteLine(Thread.CurrentThread.Name);
Thread.Sleep(1000);
}
}
在我在方法中设置之后obj
,参数仍然是一个有效的对象,为什么?
如何将参数设置为using ?null
ThreadTest
o
o
null
obj