"最后"尝试与QUOT;?;在&QUOT不是因为异步执行的 [英] "finally" not executed because of Async in "try"?

查看:233
本文介绍了"最后"尝试与QUOT;?;在&QUOT不是因为异步执行的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个布尔值( B )其中只有两次出现在我的code(除了它的声明):

I have a bool (b) which appears only twice in my code (besides its declaration):

try 
{
    b = true;
    //code including await SomeAsync();
}
catch {  }
finally { b = false; }

但有时在try块的 启动 的有 B 真正( - 的 b = TRUE ),这不应该发生,因为该声明离开它一样,在​​previous 最后。在某些时候,这个code是一个循环,这在某些情况下,快速迭代并执行 SomeAsync()正在尝试使用太多内存。我以为是扔了的的类型的异常可以的破在最后。 ( B 总是如果只有数据的正常量如预期SomeAsync( )来处理。)

yet sometimes the try block is started with b being true (-before b = true) which should never be happening because the declaration leaves it false, as does the previous finally. At some point, this code is executed in a loop, which in some cases is iterating quickly and SomeAsync() is trying to use too much memory. I assume that is throwing an exception of a type that can "break" a finally. (b is always false as expected when there's only a normal amount of data for SomeAsync() to process.)

我试过证实在Visual Studio中显示我的Debug.WriteLine()后两者在尝试和在最后,也可以通过追加到字符串中的不同角色在那些地方,但随后的最后 的执行。所以,我认为慢延迟足以prevent例外。

I've tried verifying what Visual Studio shows me with Debug.WriteLine() both after the try and after the finally, and also by appending to a string a different character in those places, but then the finally was executed. So I assume that the slow delay was enough to prevent the exception.

这是真的可能吗? (如何验证或修正它,以便最终始终运行任何想法?)

Is this really possible? (Any ideas on how to verify it or fix it so that the finally always runs?)

(finally块的 的可能会失败 - 案例:<一href=\"http://stackoverflow.com/questions/111597/conditions-when-finally-does-not-execute-in-a-net-try-finally-block\">Conditions当终于在.NET try..finally块不执行)

(finally blocks can fail - cases: Conditions when finally does not execute in a .net try..finally block )

修改

一个好点的是提出了一个注释,一个答案 - 这code可能同时在等待被执行多次。不幸的是,还有另一个细节(即这些问题的答案让我知道是中肯) - 所有迭代后 - 的状态是 B 真正。的的不受并发解释。我还添加了一个如果(b)退回; 尝试块(以避免调用异步<在previous运行时/ code>)。仍然得到留下了一个真正 B 所有迭代完成后。

A good point was raised in a comment and an answer - that this code might be executed several times concurrently during the awaits. Unfortunately, there's another detail (that those answers made me aware is pertinent) - after all iterations - the state is that b is true. That is not explained by concurrency. I have also added an if (b) return; before the try block (to avoid calling the Async while the previous is running). Still getting left with a true b after all iterations are done.

推荐答案

这是一个pretty常见的错误。在code,你表现出可以同时运行多个倍。例如,你把它连接到一个按钮单击事件,用户可以点击按钮十次连续,造成功能的十人副本运行在几乎相同的时间。

This is a pretty common mistake. The code you showed can be run multiple times concurrently. For example, it you attach it to a button click event, the user can click the button ten times in a row, causing ten copies of the function to run at nearly the same time.

在这个例子中,他们不会在字面上同时运行,是关系到UI线程,但可以与调度程序从一个实例跳跃它看到一个等待下每次重叠语句。

In this example they won't literally run at the same time, being ties to the UI thread, but they can overlap with the scheduler jumping from one instance to the next every time it sees an await statement.

如果您在后台运行这一目的(例如Thread.Start),那么每个实例都将获得自己的线程,你真的会在同一时间运行多个副本。

If you are running this in the background (e.g. Thread.Start) then each instance will get its own thread and you really will have multiple copies running at the same time.

这篇关于&QUOT;最后&QUOT;尝试与QUOT;?;在&QUOT不是因为异步执行的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆