我在下面有一段代码可以从 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 的调用未执行您委托(delegate)中的代码,而是将其排队以在 ThreadPool 线程上执行。当您的异常确实发生时,没有适当的异常处理。您是想在这里使用 Invoke 或 BeginInvoke 吗?无论哪种方式,将异常处理放在委托(delegate)中都可以解决您的问题。
关于c# - Dispatcher.Invoke 不捕获异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14595969/