By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,827 Members | 2,177 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,827 IT Pros & Developers. It's quick & easy.

"Cannot access a disposed object"

P: n/a
Hi,

I have a grid which is continuously updating by the data from a external
event. When I close the form on which the grid is placed, then it gives the
error message ...

"Can not access a disposed object".

I understand that it is because as soon as the form is closed it will in
turn try to dispose the grid. I have unwired the event in the dispose of
grid (In which event I get the data) but it doesn't solve the purpose
completely. It is because when the dispose is called for grid, control might
be inside the method (Though I have already check in the start of the method
if the instance of the grid is not null) , where the grid is accessed.
Please tell me, how I can avoid this situation and dispose the grid
properly.

Regards,
Rajat.
Jul 21 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Is there a stack trace associated with the exception?

"Rajat Tandon" <ra**********@algorismtech.comwrote in message
news:eY**************@TK2MSFTNGP03.phx.gbl...
Hi,

I have a grid which is continuously updating by the data from a external
event. When I close the form on which the grid is placed, then it gives
the
error message ...

"Can not access a disposed object".

I understand that it is because as soon as the form is closed it will in
turn try to dispose the grid. I have unwired the event in the dispose of
grid (In which event I get the data) but it doesn't solve the purpose
completely. It is because when the dispose is called for grid, control
might
be inside the method (Though I have already check in the start of the
method
if the instance of the grid is not null) , where the grid is accessed.
Please tell me, how I can avoid this situation and dispose the grid
properly.

Regards,
Rajat.


Jul 21 '06 #2

P: n/a
I have seen a few causes of this ...

Are you by chance using invoke/begininvoke to move operations to the main
thread from a background thread? If you are you have a race condition .. The
background thread doing the updating looks and the grid is fine, its uses
begininvoke or invoke to send a message to the main thread to do the update.
Both invoke and begininvoke use PostMessage to put a message for the main
thread. While the message sits in the queue the datagrid is disposed. The
main thread then picks up the message from the queue and tries to execute
it. If this is your problem you can work around it by doing the check on the
main thread as opposed to in thge background thread
be inside the method (Though I have already check in the start of the
method
if the instance of the grid is not null) , where the grid is accessed.
Also you say that you are checking that the reference is not null .. the
reference is not null but is pointing to a disposed object (its still a
valid reference and is not null, it has just been told to release anything
internal and is therefore inaccessible)

Cheers,

Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung
'
"Rajat Tandon" <ra**********@algorismtech.comwrote in message
news:eY**************@TK2MSFTNGP03.phx.gbl...
Hi,

I have a grid which is continuously updating by the data from a external
event. When I close the form on which the grid is placed, then it gives
the
error message ...

"Can not access a disposed object".

I understand that it is because as soon as the form is closed it will in
turn try to dispose the grid. I have unwired the event in the dispose of
grid (In which event I get the data) but it doesn't solve the purpose
completely. It is because when the dispose is called for grid, control
might
be inside the method (Though I have already check in the start of the
method
if the instance of the grid is not null) , where the grid is accessed.
Please tell me, how I can avoid this situation and dispose the grid
properly.

Regards,
Rajat.


Jul 21 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.