Dispatcher.Invoke 不会捕获异常
本文关键字:捕获异常 Invoke Dispatcher | 更新日期: 2023-09-27 17:56:47
我在下面有一个代码来获得HTTP GET
的响应:
private void ResponseReady(IAsyncResult aResult)
{
HttpWebRequest request = aResult.AsyncState as HttpWebRequest;
try
{
this.Dispatcher.BeginInvoke(delegate()
{
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(aResult);
当没有连接时出现问题,它将停止在response
行。它不会捕获异常。是因为Dispatcher.Invoke
吗?
未捕获异常,因为对 BeginInvoke 的调用不会执行委托中的代码,它会将其排队以在线程池线程上执行。当发生异常时,没有异常处理。你的意思是在这里使用 Invoke 还是 BeginInvoke?无论哪种方式,将异常处理放在委托中都应该可以解决您的问题。
BeginInvoke 委托中的代码在不同的线程中执行,您需要在那里创建一个单独的 try/catch。