了解InvalidAsynchronousStateException的出现 [英] understanding InvalidAsynchronousStateException occurrences

查看:383
本文介绍了了解InvalidAsynchronousStateException的出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么时候引发InvalidAsynchronousStateException?

When does InvalidAsynchronousStateException get thrown?

我有以下代码:

control.InvokeRequired吗? control.Invoke(表达式): expression();

control.InvokeRequired ? control.Invoke(expression) : expression();

在某些随机情况下,我会收到InvalidAsynchronousStateException并挂起我的应用程序,在进行一些阅读之后,似乎是在创建control的线程完成时将抛出此异常.这样对吗?如果是这样,似乎不是这样,除非有什么原因导致我的应用程序崩溃并且此异常仅仅是后果?这可能吗?

In some random cases I get InvalidAsynchronousStateException and my application hangs, after doing some reading it seems to be that this exception will be thrown when the thread where the control was created finished. Is this correct? If so, this doesn't seem to be the case, unless something is making my application crash and this exception is just a consequence? is this possible?

System.ComponentModel.InvalidAsynchronousStateException:调用该方法时发生错误.目标线程不再存在. 在System.Windows.Forms.Control.WaitForWaitHandle(WaitHandle waitHandle) 在System.Windows.Forms.Control.MarshaledInvoke(控制调用方,委托方法,Object []参数,布尔同步) 在System.Windows.Forms.Control.Invoke(委托方法,Object []参数) 在System.Windows.Forms.Control.Invoke(Delegate方法) 在c:\ Optimus \ Desktop \ Framework \ Spring \ Aspects \ UIThreadInterceptor.cs:line 22中的Optimus.Desktop.Framework.Spring.Aspects.UIThreadInterceptor.Invoke(IMethodInvocation调用)处 在Spring.Aop.Framework.AbstractMethodInvocation.Proceed() 在Spring.Aop.Framework.DynamicProxy.AdvisedProxy.Invoke处(对象代理,对象目标,类型targetType,MethodInfo targetMethod,MethodInfo proxyMethod,Object [] args,IList拦截器) 在InheritanceAopProxy_4fda07e8828744839065a154b30915ee.Dispose(布尔处置) 在System.ComponentModel.Component.Finalize()

System.ComponentModel.InvalidAsynchronousStateException: An error occurred invoking the method. The destination thread no longer exists. at System.Windows.Forms.Control.WaitForWaitHandle(WaitHandle waitHandle) at System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate method, Object[] args, Boolean synchronous) at System.Windows.Forms.Control.Invoke(Delegate method, Object[] args) at System.Windows.Forms.Control.Invoke(Delegate method) at Optimus.Desktop.Framework.Spring.Aspects.UIThreadInterceptor.Invoke(IMethodInvocation invocation) in c:\Optimus\Desktop\Framework\Spring\Aspects\UIThreadInterceptor.cs:line 22 at Spring.Aop.Framework.AbstractMethodInvocation.Proceed() at Spring.Aop.Framework.DynamicProxy.AdvisedProxy.Invoke(Object proxy, Object target, Type targetType, MethodInfo targetMethod, MethodInfo proxyMethod, Object[] args, IList interceptors) at InheritanceAopProxy_4fda07e8828744839065a154b30915ee.Dispose(Boolean disposing) at System.ComponentModel.Component.Finalize()


顺便说一句,我已经检查了这个答案,但并没有澄清我的疑问-> 推荐答案

通常,当UI线程退出后,后台线程试图调用UI线程时,通常会发生这种情况.您是不是偶然尝试在各自的线程中运行不同的表单,还是在非UI线程中将Show()表单或在显示之前从Invoke()转换为表单?

Usually this occurs when a background thread is attempting to invoke to a UI thread after the UI thread has already exited. Do you by any chance attempt to run different forms each in their own thread, or do you Show() forms from a non-UI thread, or Invoke() to a form before it is shown?

背景如下:

1)每个控件(包括窗体)都有一个句柄.这用于将控件绑定到基础的Windows GDI对象.

1) Every control (including Forms) has a handle. This is used to tie the control back to the underlying windows GDI objects.

2)通常在创建控件本身时不会创建控件的句柄.当控件首次为Show()n时,将创建该句柄.

2) The control's handle is usually not created when the control itself is created. The handle is created when the control is Show()n for the first time.

3)调用控件时,.NET API会尝试使用其句柄查找控件的UI线程. 如果尚未显示该表单,则将当前线程(调用线程)分配为UI线程.

3) When Invoking to a control, the .NET API attempts to locate the control's UI thread using it's handle. If the form has not yet been shown, the CURRENT THREAD (the invoking thread) will be assigned as the UI thread.

4)控件的UI线程应运行一个消息循环来处理该控件(例如,在执行Application.Run(someForm);时将自动发生此消息

4) The UI thread for a control is expected to run a message loop for handling that control (which happens automatically when you do, for instance, Application.Run(someForm);

5)因此,常见的错误是您从临时或线程池线程为其创建窗体F,Invoke()或BeginInvoke(),这将创建窗体的句柄,因此被分配为窗体的UI线程.然后,后台线程退出,或者被线程池终止,或者仅仅由于无法识别已被指定为UI线程而无法运行消息循环.随后,对该表的任何调用都将失败,并带有此异常.引发该异常仅是因为表单分配的"UI线程"未运行消息循环.

5) So the common mistake is that you create a form F, Invoke() or BeginInvoke() to it from a temporary or threadpool thread, which creates the form's handle and is therefore assigned as the form's UI thread. Then the background thread exits, or is terminated by the threadpool, or simply fails to run a message loop, since it is not aware that it has been designated a UI thread. Subsequently, any invocations to that form fail with this exception. The exception is thrown simply because the form's assigned 'UI thread' is not running a message loop.

有关此情况的详细分析,请参见Ivan的帖子: http: //www.ikriv.com/en/prog/info/dotnet/MysteriousHang.html

See Ivan's post for a detailed analysis of how this happens: http://www.ikriv.com/en/prog/info/dotnet/MysteriousHang.html

这篇关于了解InvalidAsynchronousStateException的出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆