我正在运行一个 while 循环,它永远跟踪一些事件,以防万一我遇到任何异常我将其引用更改为 null 希望当前线程将被中止并创建该线程的新引用。中止当前线程并启动新线程是否正确或更好的方法。
我正在尝试这样做:
Thread th;
Main()
{
th = new thread(myfunction);
th.Start();
}
void myfunction()
{
while(true)
{
try
{
// something interesting here.
}
catch(exception)
{
th = null;
}
}
}
请您参考如下方法:
唯一会发生的是 Thread 将无法从 Enclosing 类访问。
如果没有进一步的处理,这样做会使线程无法被 GC appllication roots .这使得对象可用于下一个 GC 触发器中的垃圾收集。
