为什么异步方法中的 while 循环不停止?
Why doesn't my while loop in an async method stop?
我在我的应用
的 async
方法中得到了以下代码
b_saving = true;
while (b_saving == true)
{
try
{
if (1 == 1)
b_saving = false;
// anything down here should not execute?
i_should_not_run();
}
catch{
}
finally{
}
}
我对这段代码的期望是,一旦 b_saving 设置为 false,它就会停止。然而当我 运行 它时,情况并非如此。断点显示即使 b_saving 为假,该方法也会继续。
while 循环在 C# 中不能这样工作吗?
b_saving = true;
while (b_saving == true)
{
try
{
bool stop = true;
if (stop == true)
{
b_saving = false;
break;
}
// anything down here should not execute?
i_should_not_run();
}
catch{
}
finally{
}
}
那么它会通过代码进行运行ning一次运行,直到While
检查完成。
如果您想尽早跳出循环,那么您需要一个 break;
语句。
您需要使用break
停止执行,如下所示
b_saving = true;
while (b_saving == true)
{
try
{
if (1 == 1){
b_saving = false;
break; // Will exit the loop
}
// anything down here should not execute?
i_should_not_run();
}
catch{
}
finally{
}
}
Read :
The break statement terminates the closest enclosing loop or switch statement in which it appears. Control is passed to the statement that follows the terminated statement, if any.
为了让 while 循环停止而不继续执行该方法,您的 if 语句应该是这样的:
if(1 == 1)
{
b_saving = false;
break;
}
break 关键字将退出循环。
这与异步或 try/catch 无关。
将 b_saving
设置为 false 只是故事的一部分 - 在您实际 测试它的值并根据该值采取行动之前它没有任何效果(在这个case 退出 while
循环)。
这就是 while 循环的方式 运行。
您可能还会从编译器中获得有趣的结果(取决于语言)- 一些代码可能会被删除,因为它们实际上是无用的。
即使在您将 b_saving
设置为 false 后,循环仍将继续,除非您在其中放置 break
命令,while 循环将继续执行直到其块结束,然后才执行重新评估条件,在这种情况下很可能会避免循环的另一次迭代,除非您在 i_should_not_run
方法中将 b_saving
设置回 true。
PS 你可以在while中写一个更简单的条件,不需要把==...
while (b_saving)
这个循环第一次应该执行一次b_saving == true
。在循环中,您将 b_saving
设置为 false,这样它就不会执行第二次。
要防止调用 i_should_not_run
添加 break
语句。
几点:
- 上面的代码将在设置
m_saving 标记为 false,我希望它立即停止,你应该
使用 break 关键字声明它。
- 从你使用的"m_saving"成员的名称来看,它是一个局部变量,因此在多线程场景中你应该使用原子操作机制对其进行读写。
- catch 块应该为空吗?
仅供参考的模板
为了{;;}
{
尝试
{
如果(必须停止)
{
休息;
}
}
抓住 { hasToStop = true;}
}
我在我的应用
的async
方法中得到了以下代码
b_saving = true;
while (b_saving == true)
{
try
{
if (1 == 1)
b_saving = false;
// anything down here should not execute?
i_should_not_run();
}
catch{
}
finally{
}
}
我对这段代码的期望是,一旦 b_saving 设置为 false,它就会停止。然而当我 运行 它时,情况并非如此。断点显示即使 b_saving 为假,该方法也会继续。
while 循环在 C# 中不能这样工作吗?
b_saving = true;
while (b_saving == true)
{
try
{
bool stop = true;
if (stop == true)
{
b_saving = false;
break;
}
// anything down here should not execute?
i_should_not_run();
}
catch{
}
finally{
}
}
那么它会通过代码进行运行ning一次运行,直到While
检查完成。
如果您想尽早跳出循环,那么您需要一个 break;
语句。
您需要使用break
停止执行,如下所示
b_saving = true;
while (b_saving == true)
{
try
{
if (1 == 1){
b_saving = false;
break; // Will exit the loop
}
// anything down here should not execute?
i_should_not_run();
}
catch{
}
finally{
}
}
Read :
The break statement terminates the closest enclosing loop or switch statement in which it appears. Control is passed to the statement that follows the terminated statement, if any.
为了让 while 循环停止而不继续执行该方法,您的 if 语句应该是这样的:
if(1 == 1)
{
b_saving = false;
break;
}
break 关键字将退出循环。
这与异步或 try/catch 无关。
将 b_saving
设置为 false 只是故事的一部分 - 在您实际 测试它的值并根据该值采取行动之前它没有任何效果(在这个case 退出 while
循环)。
这就是 while 循环的方式 运行。
您可能还会从编译器中获得有趣的结果(取决于语言)- 一些代码可能会被删除,因为它们实际上是无用的。
即使在您将 b_saving
设置为 false 后,循环仍将继续,除非您在其中放置 break
命令,while 循环将继续执行直到其块结束,然后才执行重新评估条件,在这种情况下很可能会避免循环的另一次迭代,除非您在 i_should_not_run
方法中将 b_saving
设置回 true。
PS 你可以在while中写一个更简单的条件,不需要把==...
while (b_saving)
这个循环第一次应该执行一次b_saving == true
。在循环中,您将 b_saving
设置为 false,这样它就不会执行第二次。
要防止调用 i_should_not_run
添加 break
语句。
几点:
- 上面的代码将在设置 m_saving 标记为 false,我希望它立即停止,你应该 使用 break 关键字声明它。
- 从你使用的"m_saving"成员的名称来看,它是一个局部变量,因此在多线程场景中你应该使用原子操作机制对其进行读写。
- catch 块应该为空吗?
仅供参考的模板
为了{;;} { 尝试 { 如果(必须停止) { 休息; } } 抓住 { hasToStop = true;} }