尝试这个:
this.Dispatcher.BeginInvoke((Action) delegate
{
User32.SendMessage(...);
}, DispatcherPriority.Input);
应用程序应该按照您希望的方式崩溃。
我见过的大多数例子都调用Dispatcher.BeginInvoke()
与匿名委托一起使用Action
.
- https://richnewman.wordpress.com/2012/12/03/tutorial-asynchronous-programming-async-and-await-for-beginners/ https://richnewman.wordpress.com/2012/12/03/tutorial-asynchronous-programming-async-and-await-for-beginners/
- http://tech.pro/tutorial/800/working-with-the-wpf-dispatcher http://tech.pro/tutorial/800/working-with-the-wpf-dispatcher
为什么会出现这种情况?
看起来 CLR 代码在下面:
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
at System.Windows.Threading.DispatcherOperation.InvokeImpl()
调用一个Action
大多数其他委托都是使用反射直接调用的。
反射机制会将异常包装在TargetInvocationException
.
See 这个答案 https://stackoverflow.com/a/28746677/4593760到另一个问题来解释一下。
其他不会包装异常的特殊情况委托有:DispatcherOperationCallback
and SendOrPostCallback
,尽管要工作它们必须用一个参数来调用。
this.Dispatcher.BeginInvoke(DispatcherPriority.Input,
(SendOrPostCallback)(delegate(object o)
{
throw new AccessViolationException(o.ToString());
}), "test");