- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我的实用程序库 (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 秒,然后我看到的下一件事是源代码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 {} block 。
另一个奇怪的是,它最后还写了以下内容:“由于先前的函数评估超时,函数评估被禁用。”我用 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 可以配置为在抛出特定异常类型时中断执行。
您可以使用 Exceptions
窗口(调试菜单)分别为每个异常类型指定调试器行为。默认情况下,所有异常都选中了“用户未处理”复选框,这意味着只有未处理 异常才会中断执行。为某种异常类型设置“抛出”复选框会强制 VS 中断执行,即使异常将被处理,但仅限于该异常类型(不适用于派生类型)。如果处理程序存在,一旦您恢复执行(通过按 F5),异常将被正常捕获。
我猜您的自定义异常已添加到异常窗口中的异常列表中(您可以使用窗口内的 Find
按钮进行检查)。
[编辑]
根据我的测试,在 .NET 4 中使用 DynamicInvoke
时也会发生这种情况,无论 Exceptions
窗口设置如何。昨天我使用的是 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 调试器无法看到该方法调用中存在异常处理程序。
关于c# - 回避异常和 "a function evaluation has timed out",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9352087/
我被卡住了,请重新审视这个问题。 我正在与其他人的意大利面条式代码一起工作,这些代码不再存在,并且有很多时间弄清楚他们正在评估什么。 在查询转储中,我看到
真实世界Haskell的第8章 globToRegex' (c:cs) = escape c ++ globToRegex' cs 这个函数不是尾递归的,它说答案依赖于 Haskell 非严格(惰性)
来自 Haskell,我正在阅读 Idris 关于懒惰(非严格)的故事。我翻了翻最近的发行说明,还有 found code类似于以下 myIf : (b : Bool) -> (t : Lazy a)
我正在读这个帖子 A custom find function并指出了这一点 有趣的结果。请注意,Evaluate 与 Application.Evaluate 相同,并且所需时间大约是 Active
R包裹mice带有以下示例: library("mice") imp <- mice(nhanes) fit <- with(data=imp,exp=lm(bmi~hyp+chl)) 我想要一个灵活
我正在尝试使用 PHPUnit 3.6.4 对我的 Zend Framework 应用程序进行单元测试。当我在命令提示符中尝试此命令时,出现以下错误。 C:\xampp\htdocs\testsamp
我希望函数的结果是: 所有值的计算结果均为 False(无、0、空字符串)-> True 所有值的计算结果为 True -> True 所有其他情况 -> 错误 这是我的尝试: >>> def con
我可以使用 puppeteer 导航到一个页面但后来page.evaluate没有返回任何响应。此外,我无法在 page.evaluate 内部进行调试。任何一个。我在 Debug模式下运行脚本( n
from surprise import Reader, Dataset, SVD from surprise import evaluate ----------------------------
使用 PhantomJS 考虑以下代码片段: var reloadAfterLogin = false; function(user, pass, debug){ // step 3 submit L
我知道流是惰性的,直到在流上调用终端方法。我所知道的是,在调用终端方法之后,所有中间方法都按调用顺序执行。 但是对于下面的程序,我无法理解流是如何工作的。这是代码,我试过了。 import java.
最近我开始研究 VS2012。 当我调试代码(这是 SharePoint 2013 应用程序)并尝试获取变量的值时 - 我一次又一次收到消息: Function evaluation disabled
我正在阅读 David Flanagan 的《JavaScript:权威指南》,这可能是世界上最厚的 JavaScript 书。在简要描述数组初始值设定项时,弗拉纳根说“每次数组初始值设定项时都会评估
在 puppeteer 截取屏幕截图之前,我试图让代码执行等待所有图像加载完毕。当调用 initData() 函数时,我的 DOM 被填充,该函数在客户端 js 文件中定义。延迟或超时是一种选择,但我
对国际通用标准有一些批评,如 [Under-attack]。 1 您认为使用 CC 开发 IT 产品的利弊是什么? 最佳答案 我是 BSI(德国)和 NIAPP(美国)计划的通用标准评估员。我有一些经
关闭。这个问题是off-topic .它目前不接受答案。 想改善这个问题吗? Update the question所以它是 on-topic对于堆栈溢出。 11 年前关闭。 Improve this
在非严格求值语言中,使用按名称调用与通过宏扩展调用有何区别和优点/缺点? 您能否提供一个示例来解释这两种评估策略? 谢谢! 最佳答案 按姓名调用: 按名称调用是一种求值策略,其中函数的参数在调用函数之
我真的无法在 Modelica 规范中找到任何答案,所以我想问问你们。规范指出如果表达式的值不影响结果(例如, bool 表达式的短路评估),工具可以自由地求解方程、重新排序表达式和不计算表达式。 I
我想尝试惰性表达式评估,但我现在不想深入研究 Haskel。拜托,你能帮忙找出其他语言有这个功能吗? 最佳答案 你可以用多种语言模拟它。 this例如,是 C++ 的通用惰性求值器。正如文章所说,它也
关注,据说foldl'是 foldl 的严格版本. 但是我很难理解,strict 是什么意思?意思是?? foldl f z0 xs0 = lgo z0 xs0 where
我是一名优秀的程序员,十分优秀!