- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当使用带有取消标记的 SemaphorSlim 时,我应该如何构造 try/finally 以便正确处理 OperationCancelledException?在选项A中,取消 token 源会抛出OperationCancelledException,但不会调用Release()。在选项B中,取消 token 源会抛出OperationCancelledException并且确实会调用Release()。
// option A:
_semaphorSlim.Wait( _cancellationTokenSource.Token );
try
{
// do work here
}
finally
{
_semaphorSlim.Release();
}
// option B:
try
{
_semaphorSlim.Wait( _cancellationTokenSource.Token );
// do work here
}
finally
{
_semaphorSlim.Release();
}
最佳答案
这里选项A更正确。取消时无需释放
SemaphoreSlim
,因为您从未真正获取并增加其计数。因此,除非 Wait
调用实际成功,否则您不想释放。
从此MSDN Page on using Semaphore and SemaphoreSlim :
It is the programmer's responsibility to ensure that a thread does not release the semaphore too many times. For example, suppose a semaphore has a maximum count of two, and that thread A and thread B both enter the semaphore. If a programming error in thread B causes it to call Release twice, both calls succeed. The count on the semaphore is full, and when thread A eventually calls Release, a SemaphoreFullException is thrown.
关于.net - SemaphoreSlim.Wait( CancellationToken ) 对OperationCancelledException 进行正确的try/finally 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6233774/
原因是如果它是CancellationToken.None,我需要添加一个超时(所以一个新的CancellationToken 来自CancellationTokenSource),例如 if (ca
我认为 RunAsync 方法采用 CancellationToken 作为参数,这很棒。 不幸的是,根据我的观察,我从未被取消。 当然,取消RunAsync 方法并调用OnCloseAsync 会有
我正在尝试使用取消标记从另一个线程中跳出循环: using System; using System.Threading; using System.Threading.Tasks; namespac
我对 AzureFunctions 还很陌生,所以请原谅我问一个简单的问题...... 我有以下代码,当队列中有一个项目时会触发该代码 [FunctionName("ProcessCompanies"
我有以下异步函数: private async Task ValidateFtpAsync() { return await Task.Run( ()
.NET 中的 TPL 新手。尝试了解 CancellationToken 以及它们如何收到取消正在执行的任务的信号。下面的代码只传输一个被取消的任务,因为相同的 token 被传递给两个任务。我的假
我有一个计时器,每 2 秒启动 2 个任务...我在一个简单的列表中跟踪这些任务(这样我就可以在停止应用程序时等待它们完成)。 任务有效地转到数据库,运行几个更新并完成。 任务本身的运行时间不会超过一
在没有搭建快速测试台的情况下;我想我会很快问 SO 看看是否有人知道这个答案。 此外,它可能还有一个额外的好处,即通知可能遇到类似情况的其他用户。 假设我有一个长期存在的 CancellationTo
我的 Xamarin.Forms 项目出现以下错误: at System.Threading.CancellationToken.ThrowOperationCanceledException (
我对如何为以下情况实现取消 token 感到有些困惑。 假设我有一个方法,它有一个取消 token ,没有像这样指定超时。 public static async Task DoSomeAsyncTh
.NET 4.5.1:看来,我无法使用 CancellationTokenSource 内置超时取消在任务中运行的阻塞方法。 class Program { static void Main(
我想使用锁或类似的同步来保护临界区。同时我想听一个CancellationToken。 现在我正在使用这样的互斥锁,但互斥锁的性能不佳。我可以使用任何其他同步类(包括新的 .Net 4.0)来代替互斥
这是我的情况: private CancellationTokenSource cancellationTokenSource; private CancellationToken c
我有一个用于各种任务的 token ,我需要更好地管理它们的取消,以便收到我可以使用的取消通知: token.Register(RegisterMethod); 我怎样才能删除这个“订阅”?有什么办法
我启动一个任务,然后启动其他任务等等。给定那棵树,如果任何任务失败,则整个操作的结果都是无用的。我正在考虑使用取消 token 。令我惊讶的是, token 没有“CancelThisToken()”
我今天早上一直在研究 Async CTP,并且有一个带有 button 和 label 的简单程序。单击按钮,它开始更新标签,停止按钮,它停止写入标签。但是,我不确定如何重置 Cancellation
我有一些异步代码,我想将其添加到 CancellationToken 中。但是,有很多实现不需要这样做,所以我想要一个默认参数 - 也许是 CancellationToken.None。然而, Tas
这段代码执行时 cancellationToken.ThrowIfCancellationRequested(); try catch block 不处理异常。 public Enumerab
最近我遇到了a Microsoft interface使用一个非常不寻常的 API: public interface IHostApplicationLifetime { public Ca
CancellationToken.CanBeCanceled 上的文档描述了它的作用,但没有提到它什么时候可能是假的。因此,我不完全清楚何时应该注意此属性。 在什么情况下它可以是假的,我应该什么时候
我是一名优秀的程序员,十分优秀!