一种常见的多线程循环逻辑的重新理解
_robotAutoLoopThread = new Thread(RobotAutoLoop) { IsBackground = true }; _robotAutoLoopThread.Name = "RobotAutoLoopThread"; _robotAutoLoopThread.Start(); private void RobotAutoLoop() { while (!_isRobotAutoLoopStopped) { //循环内容 } }
以上代码中,_robotAutoLoopThread 这个线程启动后,
一旦_isRobotAutoLoopStopped=true,循环线程会立即退出;
之后即使 _isRobotAutoLoopStopped 再被改回 false,
这个线程已经结束,不会自动继续循环。
若想重新开始循环,必须手动重新启动线程。

浙公网安备 33010602011771号