c# - Cannot access a disposed object -


i facing huge problem "cannot access disposed object. object name: 'treeview'." error.

on windows forms of mine use custom windows explorer object.

and here come code parts...

on selected node event, load images found within selected directory flowlayoutpanel.

 private sub exptree1_exptreenodeselected(byval selpath string, byval item explorercontrols.cshitem) handles exptree1.exptreenodeselected       'loop until images loaded.        loadimagestoflowpreviewpanel()  end sub 

on button close event

 private sub wizardcontrol1_cancelclick(byval sender system.object, byval e system.componentmodel.canceleventargs) handles wizardcontrol1.cancelclick         me.close()  end sub 

on form closing event

 private sub wizard_formclosing(byval sender object, byval e system.windows.forms.formclosingeventargs) handles me.formclosing                 select case xtramessagebox.show("exit application?", me.text, messageboxbuttons.yesno, messageboxicon.question)                 case windows.forms.dialogresult.no                     e.cancel = true                 end select         end if  end sub 

while debugging noticed when confirm closing application, code within loadimagestoflowpreviewpanel sub continues execute. error raised when images loaded flowlayoutpanel control.

and here comes stack trace...

   @ system.windows.forms.control.createhandle()    @ system.windows.forms.treeview.createhandle()    @ system.windows.forms.control.get_handle()    @ system.windows.forms.treeview.tvnselected(nmtreeview* nmtv)    @ system.windows.forms.treeview.wmnotify(message& m)    @ system.windows.forms.treeview.wndproc(message& m)    @ system.windows.forms.control.controlnativewindow.wndproc(message& m)    @ system.windows.forms.nativewindow.debuggablecallback(intptr hwnd, int32 msg, intptr wparam, intptr lparam)    @ system.windows.forms.unsafenativemethods.sendmessage(handleref hwnd, int32 msg, intptr wparam, intptr lparam)    @ system.windows.forms.control.sendmessage(int32 msg, intptr wparam, intptr lparam)    @ system.windows.forms.control.reflectmessageinternal(intptr hwnd, message& m)    @ system.windows.forms.control.wmnotify(message& m)    @ system.windows.forms.control.wndproc(message& m)    @ system.windows.forms.application.parkingwindow.wndproc(message& m)    @ system.windows.forms.control.controlnativewindow.wndproc(message& m)    @ system.windows.forms.nativewindow.debuggablecallback(intptr hwnd, int32 msg, intptr wparam, intptr lparam)    @ system.windows.forms.unsafenativemethods.callwindowproc(intptr wndproc, intptr hwnd, int32 msg, intptr wparam, intptr lparam)    @ system.windows.forms.nativewindow.defwndproc(message& m)    @ system.windows.forms.treeview.wmmousedown(message& m, mousebuttons button, int32 clicks)    @ system.windows.forms.treeview.wndproc(message& m)    @ system.windows.forms.control.controlnativewindow.wndproc(message& m)    @ system.windows.forms.nativewindow.debuggablecallback(intptr hwnd, int32 msg, intptr wparam, intptr lparam)    @ system.windows.forms.unsafenativemethods.dispatchmessagew(msg& msg)    @ system.windows.forms.application.componentmanager.system.windows.forms.unsafenativemethods.imsocomponentmanager.fpushmessageloop(int32 dwcomponentid, int32 reason, int32 pvloopdata)    @ system.windows.forms.application.threadcontext.runmessageloopinner(int32 reason, applicationcontext context)    @ system.windows.forms.application.threadcontext.runmessageloop(int32 reason, applicationcontext context)    @ microsoft.visualbasic.applicationservices.windowsformsapplicationbase.onrun()    @ microsoft.visualbasic.applicationservices.windowsformsapplicationbase.doapplicationmodel()    @ microsoft.visualbasic.applicationservices.windowsformsapplicationbase.run(string[] commandline)    @ cannonupdater.my.myapplication.main(string[] args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 82    @ system.appdomain._nexecuteassembly(assembly assembly, string[] args)    @ system.runtime.hosting.applicationactivator.createinstance(activationcontext activationcontext, string[] activationcustomdata)    @ microsoft.visualstudio.hostingprocess.hostproc.runusersassemblydebuginzone()    @ system.threading.executioncontext.run(executioncontext executioncontext, contextcallback callback, object state)    @ system.threading.threadhelper.threadstart() 

update: if images loaded flowlayoutpanel , confirmation of application close comes next, no error.

you should post relevant parts of loadimagestoflowpreviewpanel method.

this speculation without seeing code, 1 explanation be:

  • loadimagestoflowpreviewpanel calling application.doevents in loop

  • the form closed , treeview disposed during 1 of calls application.doevents

  • but loop continues execute , accesses disposed treeview.

if this, solution either redesign avoid calling application.doevents, or @ least checking if form closed / treeview disposed after every call application.doevents.

update in response comment:

wow! loadimages calling application.doevents within loop

if use application.doevents expose reentrancy problems in code - need careful , make sure understand consequences when use it. issue described not problem you're face. use in specific cases can guarantee not have reentrancy problems (e.g. while modal progress dialog displayed). , many people call doevents "evil" , have nothing @ all.


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -