Difference between CancellationTokenSource and exit flag for Task loop exit
问题 I was wondering if there is any difference between ending loop task with CancellationTokenSource and exit flag CancellationTokenSource: CancellationTokenSource cancellationTokenSource; Task loopTask; void StartLoop() { cancellationTokenSource = new CancellationTokenSource(); loopTask = Task.Factory.StartNew(Loop, TaskCreationOptions.LongRunning); } void Loop() { while (true) { if (cancellationTokenSource.IsCancellationRequested) break; Thread.Yield(); } } void StopLoop() {