Available object not available

I ran into a huge problem with the error "Cannot access a disposed object. Object name: 'TreeView'." .

In the windows of my form, I use a custom Windows Explorer object .

And here comes the pieces of code ...

In the selected node event, I load the images found in the selected directory into the 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 

In button close event

  Private Sub WizardControl1_CancelClick(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles WizardControl1.CancelClick Me.Close() End Sub 

In the form close event

  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 

During debugging, I noticed that when I confirm the application is closed, the code within the framework of LoadImagesToFlowPreviewPanel continues to run. The error occurs when all images are loaded into the FlowLayoutPanel control.

And here comes the stack trace ...

  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() 

UPDATE: IF all images are uploaded to FlowLayoutPanel and the confirmation of closing the application will be as follows, I will not get an error.

+4
source share
3 answers

You must publish the appropriate parts of your LoadImagesToFlowPreviewPanel method.

This is an assumption without seeing the code, but one explanation may be:

  • LoadImagesToFlowPreviewPanel calls Application.DoEvents in a loop

  • The form is closed and the TreeView located during one of the Application.DoEvents calls

  • But the loop continues to execute and accesses the remote TreeView.

If so, the solution would be either a redesign to avoid calling Application.DoEvents , or at least checking if the / TreeView form is closed after every call to Application.DoEvents .

UPDATE in response to the comment:

Wow! Actually LoadImages calls Application.DoEvents in a loop

If you use Application.DoEvents , you expose yourself to reconnection problems in your code - you need to be very careful and make sure that you understand all the consequences when you use it. The problem you described is not the only problem you are likely to encounter. I would use it only in special cases when I can guarantee that you will not have problems with the return (for example, while a modal progress dialog is displayed). And many people will call "evil" and have nothing to do with it.

+4
source

This mainly happens when you perform an operation on an object that is still alive that you selected. So like this:

 A a = new A(); a.Dispose(); //operations performed on a will fail now 

Or like that

 using( A a = new A()){ ... } //operations performed on a will fail now 

Remember that the block used will also delete your object in the same way as manually calling Dispose.

+3
source

Now you start the stream that controls the TreeView object, and then before the stream is executed, the TreeView will be located.

To fix this, check if the TreeView is available for management or not using the TreeView IsDisposed in your stream.

0
source

Source: https://habr.com/ru/post/1339157/


All Articles