一种常见的多线程循环逻辑的重新理解

_robotAutoLoopThread = new Thread(RobotAutoLoop) { IsBackground = true };
_robotAutoLoopThread.Name = "RobotAutoLoopThread"; 
_robotAutoLoopThread.Start();

private void RobotAutoLoop()
{
    while (!_isRobotAutoLoopStopped)
    {   
        //循环内容
    }
} 

以上代码中,_robotAutoLoopThread 这个线程启动后,

一旦_isRobotAutoLoopStopped=true,循环线程会立即退出;

 

之后即使 _isRobotAutoLoopStopped 再被改回 false

这个线程已经结束,不会自动继续循环

若想重新开始循环,必须手动重新启动线程。

posted @ 2026-08-28 18:24  尼库拉西  阅读(5)  评论(0)    收藏  举报