RichTextBox on Form -- Exception.. Cannot access a disposed object

G

Guest

I have a C# MDI app. The child forms do alot of work, so this work is
perfomed on a different thread created using ThreadPool.QueueUserWorkItem().
Because the status of the work is important to the user, I post the results
in a richTextBox on the child form. I have created an event to do this since
it is best to update the controls using the thread that they were created
with (or so I've read).

Anyway, the first time the user selects a form and starts the work (pushes
start button), everything is fine. However, if the user closes the form and
then reopens it and restarts the test, the following exception is thrown ...
"System.ObjectDisposedException" ... "Cannot access a disposed object named
"RichTextBox".".

The child form is created using this code...
private void menuItemTestMultiple_Click(object sender, System.EventArgs e)
{
dlgDATestMult = new MultiplePass(ParentWindow);
dlgDATestMult.Show();
}

The richTextBox I speak of was created using the forms designer.

Any ideas?

Thanks in advance
 
W

Wessel Troost

"System.ObjectDisposedException" ... "Cannot access a disposed object
named
"RichTextBox".".
Perhaps your thread is trying to access the RichTextBox on the old form?
If the old form was closed and disposed, you would need to update the
RichTextBox reference in the thread.

Greetings,
Wessel
 
G

Guest

Thanks for your quick reply...

I'm kinda new to this C# thing, but isn't a new instance of the form class
created each time the "new" keyword is used? If so, wouldn't that mean that
the richTextBox that is created the second time the user selected to open it
is a different text box than the first time?

If not, how do I make it different?

Thanks again
--mthgk
 
K

Kevin Yu [MSFT]

Hi mthgk,

Yes, as you assumed, each time the new keyword is used, a new instance of
the object is created. So the RichTextBox object in the new child form is
also another instance. As Wessel suggested, you need to update the
reference to RichTextBox in the working thread.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
G

Guest

Ah...........the lightbulb finally came on.... I wasn't using the "this"
keyword when invoking the event. Thanks for your help

-- mthgk
 
K

Kevin Yu [MSFT]

Nice to hear that you have had the problem resolved.

Thanks for sharing your experience with all the people here. If you have
any questions, please feel free to post them in the community.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 

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

Top