Correct way to close a form

  • Thread starter Thread starter p19010101
  • Start date Start date
P

p19010101

Basically I need to populate a form's listview with entries from a DB
table, the form is called by the main form. All these is fine.

However, the population may take a long time due to the size of the
table, so I chucked in the DoEvents method in the loop to allow screen
repaint and such.

The problem comes when the user decided to close the form instead of
waiting, that's when the "Cannot Access A Disposed Object" exception
pops up. I understand the reason behind is that the listview is not
longer there. Is there a way to handle this?
 
Hi,

You could create a form level boolean bClosing and set it false when
you start the loop. In the form closing event set it to true. In your loop
exit the loop when bClosing=true. I would also put the line of code where
you add the data to the listview in a try catch block to catch any exception
that might occur.

Ken
 
You can try to avoid DoEvents and use ListView.Refresh.

Or use a separate thread if you want to handle events to cancel, etc. See
the 3 articles of the series:

Safe, Simple Multithreading in Windows Forms
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnforms/html/winforms01232003.asp

Also, you can check if the listview is still there with the IsDisposed
property of all windows controls.

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top