我遇到了"Cannot access a disposed object. Object name: 'TreeView'."
错误的巨大问题。
在我的Windows窗体上,我使用custom windows explorer object。
这里是代码部分......
在选定的节点事件上,我将在所选目录中找到的图像加载到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
sub中的代码继续执行。将所有图像加载到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
,接下来确认应用程序已关闭,则不会出现错误。
答案 0 :(得分:4)
您应该发布LoadImagesToFlowPreviewPanel
方法的相关部分。
这是在没有看到代码的情况下推测,但一种解释可能是:
LoadImagesToFlowPreviewPanel在循环中调用Application.DoEvents
关闭表单并在对Application.DoEvents
但循环继续执行并访问已处理的TreeView。
如果是这样,解决方案将是重新设计以避免调用Application.DoEvents
,或者至少检查表单是否已关闭/ TreeView是否在每次调用Application.DoEvents
后被处理。
更新以回应评论:
哇!实际上,LoadImages正在循环中调用Application.DoEvents
如果您使用Application.DoEvents
使自己面临代码中的重入问题 - 您需要非常小心并确保在使用它时了解所有后果。您描述的问题不是您可能面临的唯一问题。我只会在非常特殊的情况下使用它,我可以保证不会出现重入问题(例如,在显示模态进度对话框时)。许多人会称DoEvents
为“邪恶”而与此毫无关系。
答案 1 :(得分:3)
基本上,当您对仍处于活动状态的对象执行操作时会发生这种情况。所以要么像这样:
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
请记住,使用块也会像手动调用Dispose一样处理对象。
答案 2 :(得分:0)
这里发生的是你启动一个操作TreeView对象的线程,然后在线程完成之前,TreeView被释放。
要解决此问题,请检查TreeView是否可以使用TreeView的IsDisposed
属性进行操作或处理。