【发布时间】:2011-10-20 09:02:09
【问题描述】:
我有一个附加到 Excel 互操作组件中的事件的委托。目标是使用来自 Excel 的更新信息来更新 winforms 控件。由于我正在更改 Control 属性,因此我需要使用 Invoke:
public delegate void DataGridViewUpdate(object[,] data);
...
excel.InteractiveEdit( delegate(object[,] data) {
Invoke(new Common.DataGridViewUpdate(back_from_excel), new object[] { data });
});
...
private void back_from_excel(object[,] data) {
// datagridview updating code
// an exception is thrown here !
}
(此代码在我正在更新的 Form 类中,因此它在 this 上调用)
基本上我的问题是当back_from_excel(object[,] data) 方法中发生异常时,调试器不会捕获它。我知道委托在正确的 UI 线程中运行,因为我在操作表单控件时没有问题。
具体情况是,当back_from_excel 遇到未处理的异常时,它会在该点停止执行。应用程序的其余部分继续运行并做出响应。调试器不会暂停。输出窗格显示:
A first chance exception of type 'System.NullReferenceException' occurred in My Application.exe
A first chance exception of type 'System.NullReferenceException' occurred in System.Windows.Forms.dll
它没有给我任何关于哪一行导致问题的提示,只是它在 .exe 中的某个地方。
我的问题是:我在做Invoke 的事情对吗?将委托 - 调用 - 委托像这样链接在一起似乎有点奇怪,但我确实需要传递一个调用委托的委托。我的问题是在 Visual Studio 中而不是在 C# 中吗?如果是这样,我如何将调试器重新附加到该 UI 线程?
【问题讨论】:
标签: c# visual-studio exception visual-studio-debugging