[英]Cannot access a disposed object
我面临"Cannot access a disposed object. Object name: 'TreeView'."
的巨大问题"Cannot access a disposed object. Object name: 'TreeView'."
错误。
在我的Windows窗体上,我使用自定义Windows资源管理器对象 。
这里是代码部分...
在选定的节点事件上,我将在选定目录内找到的图像加载到FlowLayoutPanel。
Private Sub ExpTree1_ExpTreeNodeSelected(ByVal SelPath As String, ByVal Item As ExplorerControls.CShItem) Handles ExpTree1.ExpTreeNodeSelected
'Loop until all images are loaded.
LoadImagesToFlowPreviewPanel()
End Sub
在按钮关闭事件上
Private Sub WizardControl1_CancelClick(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles WizardControl1.CancelClick
Me.Close()
End Sub
结束表格活动
Private Sub Wizard_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Select Case XtraMessageBox.Show("Exit the application?", Me.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question)
Case Windows.Forms.DialogResult.No
e.Cancel = True
End Select
End If
End Sub
在调试时,我注意到当我确认关闭应用程序时, LoadImagesToFlowPreviewPanel
子代码中的代码将继续执行。 当所有图像都加载到FlowLayoutPanel控件时,将引发错误。
这是堆栈跟踪...
at System.Windows.Forms.Control.CreateHandle()
at System.Windows.Forms.TreeView.CreateHandle()
at System.Windows.Forms.Control.get_Handle()
at System.Windows.Forms.TreeView.TvnSelected(NMTREEVIEW* nmtv)
at System.Windows.Forms.TreeView.WmNotify(Message& m)
at System.Windows.Forms.TreeView.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.SendMessage(HandleRef hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.Control.SendMessage(Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.Control.ReflectMessageInternal(IntPtr hWnd, Message& m)
at System.Windows.Forms.Control.WmNotify(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.Application.ParkingWindow.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
at System.Windows.Forms.TreeView.WmMouseDown(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.TreeView.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at CannonUpdater.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 82
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
更新:如果所有图像都已加载到FlowLayoutPanel
且接下来是应用程序关闭的确认,则我不会出错。
您应该发布LoadImagesToFlowPreviewPanel
方法的相关部分。
这是推测,没有看到代码,但是一种解释可能是:
LoadImagesToFlowPreviewPanel正在循环调用Application.DoEvents
在对Application.DoEvents
的调用之一期间,关闭了窗体,并放置了TreeView。
但是循环将继续执行并访问已放置的TreeView。
如果是这样,解决方案将是重新设计以避免调用Application.DoEvents
,或者至少在每次调用Application.DoEvents
之后检查是否关闭了Form / TreeView是否已处置。
更新以回应评论:
哇! 实际上,LoadImages正在循环内调用Application.DoEvents
如果使用Application.DoEvents
,则会使自己陷入代码中的重入问题-您需要非常小心,并确保您理解使用它时的所有后果。 您描述的问题并不是您可能会面对的唯一问题。 我只会在可以保证不会出现重入问题的非常特定的情况下使用它(例如,在显示模式进度对话框时)。 许多人将DoEvents
称为“邪恶的”,而与之完全无关。
基本上,当您对已处置的仍在运行的对象执行操作时,会发生这种情况。 所以要么像这样:
A a = new A();
a.Dispose();
//operations performed on a will fail now
或者像这样
using( A a = new A()){
...
}
//operations performed on a will fail now
请记住,using块也将处置对象,就像手动调用Dispose一样。
这里发生的事情是启动一个操作TreeView对象的线程,然后在完成该线程之前,先处理TreeView。
要解决此问题,请在线程中使用TreeView的IsDisposed
属性检查TreeView是否可操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.