加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 逃避异常和“功能评估超时”

发布时间:2020-12-16 01:51:17 所属栏目:百科 来源:网络整理
导读:背景 在我的实用程序库(Shd.dll)中,我有一个名为AsyncOperation的类.简而言之,它是类型的基类,它封装了可能长时间运行的操作,在后台线程上执行它,并且它支持暂停/恢复,取消和进度报告. (它就像一个BackgroundWorker,只知道更多东西.) 在用户代码中,您可以像
背景

在我的实用程序库(Shd.dll)中,我有一个名为AsyncOperation的类.简而言之,它是类型的基类,它封装了可能长时间运行的操作,在后台线程上执行它,并且它支持暂停/恢复,取消和进度报告. (它就像一个BackgroundWorker,只知道更多东西.)

在用户代码中,您可以像这样使用它:

class MyOperation : AsyncOperation
{
    public MyOperation() : base(null,AsyncOperationOptions.Cancelable | AsyncOperationOptions.Pausable) {}

    protected override void RunOperation(AsyncOperationState operationState,object userState)
    {
         ...
         operationState.ThrowIfCancelled();
    }
}

 var op = new MyOperation();
 op.Start();
 ...
 op.Cancel();

operationState.ThrowIfCancelled()正如它的名字所暗示的那样:如果另一个线程先前调用了Cancel(),它会抛出一个内部异常(AsyncOperationCancelException),然后由AsyncOperation类型处理,如下所示:

private void _DoExecute(object state)
{
    // note that this method is already executed on the background thread
    ...
    try
    {
        operationDelegate.DynamicInvoke(args); // this is where RunOperation() is called
    }
    catch(System.Reflection.TargetInvocationException tiex)
    {
        Exception inner = tiex.InnerException;
        var cancelException = inner as AsyncOperationCancelException;
        if(cancelException != null)
        {
             // the operation was cancelled
             ...
        }
        else
        {
            // the operation faulted
            ...
        }
        ...
    }
    ...
}

这非常有效.或者在过去的一年里我想到了,而我在很多场景中使用它.

实际问题

我正在构建一个使用System.Net.WebClient通过FTP上传潜在大量文件的类.此类是使用AsyncOperation基类构建的,如上所述.

对于准确的进度报告,我使用WebClient.UploadFileAsync(),这使代码复杂化,但相关部分如下所示:

private ManualResetEventSlim completedEvent = new ManualResetEventSlim(false);

private void WebClient_UploadProgressChanged(object sender,UploadProgressChangedEventArgs e)
{
    ...
    if (OperationState.IsCancellationRequested)
    {
        _GetCurrentWebClient().CancelAsync();
    }
}

private void WebClient_UploadFileCompleted(object sender,UploadFileCompletedEventArgs e)
{
    ...
    _UploadNextFile();
}

private void _UploadNextFile()
{
    if (OperationState.IsCancellationRequested || ...)
    {
        this.completedEvent.Set();
        return;
    }
    ...
}

protected override void RunOperation(AsyncOperationState operationState,object userState)
{
    ...
    _UploadNextFile();
    this.completedEvent.Wait();

    operationState.ThrowIfCancelled(); // crash
    ...
}

如您所见,我标记了发生崩溃的行.究竟发生了什么,当执行命中该行时(我在它上方放置了一个断点,所以我知道这是确切的行),Visual Studio 2010冻结了大约15秒,然后接下来我看到的是源代码of AsyncOperationState.ThrowIfCancelled():

public void ThrowIfCancelled()
{
    if(IsCancellationRequested)
    {
        throw new AsyncOperationCancelException();
    }
} // this is the line the debugger highlights: "An exception of type AsyncOperationCancelException' occured in Shd.dll but was unhandled by user code."

我尝试将断点放到应该捕获异常的位置,但执行永远不会到达catch {}块.

另一个奇怪的是,最后它还写了以下内容:“功能评估已禁用,因为之前的功能评估已超时.”我用Google搜索了这个问题,并尝试了所有建议(禁用隐式属性评估,删除了所有断点),但到目前为止没有任何帮助.

以下是两个说明问题的屏幕截图:
http://dl.dropbox.com/u/17147594/vsd1.png
http://dl.dropbox.com/u/17147594/vsd2.png

我正在使用.NET 4.0.任何帮助将非常感谢.

解决方法

当Visual Studio调试器附加到应用程序时,只要在运行的代码有机会处理它之前抛出异常,它就会得到通知.这称为 first-chance exception,VS可以配置为在抛出某个异常类型时中断执行.

您可以使用“异常”窗口(“调试”菜单)分别为每种异常类型指定调试器行为.默认情况下,所有异常都选中了“User-unhandled”复选框,这意味着只有未处理的异常才会中断执行.设置某个异常类型的“Thrown”复选框会强制VS中断执行,即使将处理异常,但仅限于该异常类型(不适用于派生类型).如果存在处理程序,则一旦恢复执行(按F5),异常将被正常捕获.

我猜你的自定义异常被添加到Exceptions窗口中的异常列表中(可以使用窗口内的Find按钮进行检查).

[编辑]

根据我的测试,无论Exceptions窗口设置如何,在.NET 4中使用DynamicInvoke时也会发生这种情况.昨天我使用的是VS2008,我无法重现它,但现在看起来确实很奇怪.

这是我试过的测试(抱歉简短的格式化,但它很简单):

Action<int> a = i => { throw new ArgumentException(); };

// When the following code is executed,VS2010 debugger
// will break on the `ArgumentException` above 
// but ONLY if the target is .NET 4 (3.5 and lower don't break)
try { a.DynamicInvoke(5); }
catch (Exception ex)
{ }

// this doesn't break
try { a.Invoke(5); }
catch (Exception ex)
{ }

// neither does this
try { a(5); }
catch (Exception ex)
{ }

我唯一的猜测是在InvokeMethodFast(这是一个InternalCall方法)内部完成的异常处理已经有所改变. DynamicInvoke代码在版本4和之前版本之间已经发生了变化,但没有任何内容可以说明为什么VS2010调试器无法看到该方法调用中存在异常处理程序.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读