I have the following code, where I start a Thread
using a ParameterizedThreadStart
object as constructor argument:
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);
}
}
After I set obj
to null
in the ThreadTest
method the argument o
is still a valid object, why?
How can I set the argument o
to null
using obj
?