Windows Develop Bookmark and Share   
 index > Windows Forms Designer > Cannot access a disposed object.
 

Cannot access a disposed object.

Hi

I have a problem with a winforms app. I have a timer (autoReset) which on elapsed updates a lable on my form. When I close the form I get an error stating "Cannot access a disposed object"

Here is the method I call on the timer event:

private void upDateTimeData()
{
if (!(this.Disposing))
{
try
{
if (this.InvokeRequired)
{
{
this.Invoke((MethodInvoker)delegate() { this.upDateTimeData(); });
}
}
else
{
label_TCGDate.Text = masterClock.TCGDate.ToShortDateString();
label_TCGTimecode.Text = masterClock.TCG.ToString();
}
}
catch (Exception e)
{
Console.WriteLine("Form Error: " + e.Message);
//Form Closing.
}

}
}

I have tried unregestering the timer event in the form closing event, but that did not help.

Does anyone know of the best way of dealing with cross thread delegation and form closing?

Thanks for your time Guy's.

John.

ltheONEl  Wednesday, June 10, 2009 12:34 PM
Hi John,

My suggestion is to stop the timer when the form is closed. Handle the FormClosed event of the Form and call the Stop method on the timer.

Please try my suggestion and let me know the result.

Sincerely,
Linda Liu
Please remember to mark the replies as answers if they help and unmark them if they provide no help. end us any feedback you have about the help from MSFT at fbmsdn@microsoft.com.
Linda Liu  Wednesday, June 17, 2009 3:28 AM
Hi Linda,

Thanks for your reply.

Your suggestion was the very first thing I tried. Unfortunately that did not work for me.

I think it may be due to the rate of the timer frame rate 1/25 second,and the formMessage pumps (if thats the correct term). I think I need a way of flushing all queues after detaching the event, and then unload the form. Just not really sure how to do that.

best regards

John
ltheONEl  Thursday, June 18, 2009 5:49 PM

You can use google to search for other answers

Custom Search

More Threads

• Use my DataSet from Designer?
• New Submission to Control Gallery
• DataGrid Headers
• Persist Hashtable property
• Add design time support to wrapped user controls
• User Control Events?
• Tree View User Control Help...
• Creating see-thru forms?
• Custom Control and Designer Refresh issues
• Adding code to VS 2005 generated windows form.