gpt4 book ai didi

c# - 当我们将线程的引用更改为 null 时会发生什么?

转载 作者:太空宇宙 更新时间:2023-11-03 22:00:05 24 4
gpt4 key购买 nike

我正在运行一个 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 触发器中的垃圾收集。

关于c# - 当我们将线程的引用更改为 null 时会发生什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10545718/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com