try{ SomeMethod(token);}catch (OperationCancelledException){ return false;}// ...voID SomeMethod(CancellationToken token){ // ... // I don't want the deBUGger to stop on the following line #pragma ignore(OperationCancelledException,true) token.ThrowIfCancellationRequested(); #pragma ignore(OperationCancelledException,false)}
我使用假设#pragma ignore来说明我的意思,但是这样做是否真的存在?
更新以解决“你所要求的不清楚”的封闭投票.在调试器中尝试此代码:https://dotnetfiddle.net/npMk6r.确保在Ctrl-Alt-E对话框中启用了所有异常.调试器将在循环的每次迭代时停止抛出新的OperationCanceledException(“cancelled1”)行.我不想让它发生,因为它是恼人的.但是,我想让它停止在循环之外的最后一次抛出,抛出新的OperationCanceledException(“cancelled2”)(或其他任何地方).
解决方法 这可能不是你正在寻找的,但是我会使用DebuggerNonUserCode
属性. 为了说明这一点,这是fiddle的修改版本.即使在Ctrl Alt E Exceptions对话框中启用了OperationCanceledException,调试器也不会停止ThrowIfCancellationRequested.
using System;using System.Diagnostics;using System.Threading;namespace TestApp{ static class Ext { [System.Diagnostics.DeBUGgerNonUserCode()] public static bool TryThrowIfCancellationRequested( this CancellationToken token) { try { // deBUGger won't stop here,because of DeBUGgerNonUserCode attr token.ThrowIfCancellationRequested(); return true; } catch (OperationCanceledException) { return false; } } } public class Program { static bool SomeMethod(CancellationToken token) { System.Threading.Thread.Sleep(1000); return token.TryThrowIfCancellationRequested(); } public static voID Main() { var cts = new CancellationTokenSource(1000); for (var i = 0; i < 10; i++) { if (!SomeMethod(cts.Token)) break; } } }}
当然,在这种特殊情况下,您可以使用CancellationToken.IsCancellationRequested而不是ThrowIfCancellationRequested,但上述方法说明了可以扩展到任何其他异常的概念.
总结以上是内存溢出为你收集整理的c# – 选择性地防止调试器停止第一次机会异常全部内容,希望文章能够帮你解决c# – 选择性地防止调试器停止第一次机会异常所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)