Luc*_*ler 5 c# delegates exception
我有这个代码:
using System;
using System.Runtime.Remoting.Messaging;
class Program {
static void Main(string[] args) {
new Program().Run();
Console.ReadLine();
}
void Run() {
Action example = new Action(threaded);
IAsyncResult ia = example.BeginInvoke(new AsyncCallback(completed), null);
// Option #1:
/*
ia.AsyncWaitHandle.WaitOne();
try {
example.EndInvoke(ia);
}
catch (Exception ex) {
Console.WriteLine(ex.Message);
}
*/
}
void threaded() {
throw new ApplicationException("Kaboom");
}
void completed(IAsyncResult ar) {
// Option #2:
Action example = (ar as AsyncResult).AsyncDelegate as Action;
try {
example.EndInvoke(ar);
}
catch (Exception ex) {
Console.WriteLine(ex.Message);
}
}
}
Run Code Online (Sandbox Code Playgroud)
在许多文章中,当我打电话时BeginInvoke,所有Exception的(这里来自方法threaded)等到我打电话,EndInvoke然后将被扔到那里.但它不起作用,Exception("Kaboom")是"未处理",程序崩溃.你能帮助我吗?
谢谢!
效果很好。当你说“程序崩溃”时,我想知道你是否只是将 IDE 设置为在所有异常时中断。我没有遇到任何崩溃 - 它会向控制台写入“Kaboom”,正如我们所期望的那样。尝试在 IDE 外部运行它或按ctrl+f5而不是仅按f5。
我认为您只是看到 IDE“有帮助”:

忽略这一点;IDE 并不总是正确的。那还是处理一下吧。
| 归档时间: |
|
| 查看次数: |
1017 次 |
| 最近记录: |