473,416 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

Correct way to close a form

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?

Nov 23 '05 #1
2 1332
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
-------------------
<p1*******@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
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?

Nov 23 '05 #2
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/de...ms01232003.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

<p1*******@yahoo.com> escribió en el mensaje
news:11**********************@f14g2000cwb.googlegr oups.com...
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?

Nov 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: marcelf3 | last post by:
Hello.. This page opens a window with some information, but everytime the user changes a field in the parent window, the child window needs to be closed. These 2 functions were supposed to do the...
4
by: Niyazi | last post by:
Hi I am trying to insert some value into SQL Server 2000 tables and I am keep getting the "Input string was not in a correct format" error. When user fills the Form it updates the table call...
1
by: Jules Winfield | last post by:
My WinForms application can have any number of top level forms open at a given time. If the user selects File|Exit, all of the forms are closed. The loop to close the forms looks something like this:...
23
by: jm | last post by:
This works, but I don't know why. static void Main() { // Queue the task. ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc),new Form1()); Application.Run(); }
4
by: Joe Kimbler | last post by:
I've heard this both ways so I'm wondering, is using the: Me.Dispose () ....method in VB.NET the best way to close down an application? Is there a cleaner way to do it? I've heard grumblings...
5
by: Alex | last post by:
Hey all - I've found a lot of 'snippet' examples of hitting a sqlserver db from a csharp windows app, but I want to make sure I understand the proper usage. In the examples, they usually...
0
by: MikeY | last post by:
Hi everyone, I posted a question very early today, but maybe I wasn't articulate enough. Hopefully this will illicit a response I've created a windows form and a User Control. This is a...
2
by: sonasiva | last post by:
hai I need to write a code for register page of my website i have written the following code, <% DIM strusername, strpassword, stremail strUsername =request.form("username") strPassword =...
15
by: Andyza | last post by:
I'm looking at another developers code and I'm now confused about the correct way to close a database connection and destroy the object. I would normally do it this way: Set conn =...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.