PC Review


Reply
Thread Tools Rate Thread

"Cannot access a disposed object"

 
 
Rajat Tandon
Guest
Posts: n/a
 
      21st Jul 2006
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.


 
Reply With Quote
 
 
 
 
Marina Levit [MVP]
Guest
Posts: n/a
 
      21st Jul 2006
Is there a stack trace associated with the exception?

"Rajat Tandon" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.
>
>



 
Reply With Quote
 
Greg Young
Guest
Posts: n/a
 
      21st Jul 2006
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Cannot access a disposed object named "System.Net.TlsStream". SMike Microsoft C# .NET 0 22nd Sep 2004 08:29 PM
Cannot access a disposed object named "System.Net.TlsStream"? Asaf Microsoft C# .NET 0 4th Mar 2004 04:01 PM
Cannot access a disposed object named "System.Net.TlsStream"? Asaf Microsoft ASP .NET 0 3rd Mar 2004 08:31 AM
Cannot access a disposed object named "System.Net.TlsStream"? Asaf Microsoft C# .NET 0 18th Dec 2003 06:10 PM
Cannot access a disposed object named "System.Net.TlsStream"? Asaf Microsoft ASP .NET 0 18th Dec 2003 01:05 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:27 AM.