473,382 Members | 1,745 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,382 software developers and data experts.

Exception when Form object is accessed after being disposed

Hi,

The problem is that the line -
this.Invoke(new MyDelegate(Function), args); // this = MainForm
is being called after the line -
terminatePopulate = true;

therefore I get an exception which states that MainForm has been
disposed when this.Invoke is called...

Is there a way to check if MainForm has been disposed. Perhaps theres
a better way of doing what am trying to achieve,

Thanks,

Aine

public class FormMain : Form
{
volatile bool terminatePopulate = false;

void FormMain_Load()
{

myThread = new System.Threading.Thread(MyThread());
myThread.Start();
}

void MyThread()
{

while (!anotherObject.ContinueWaiting)
{

if (!terminatePopulate)
this.Invoke(new MyDelegate(Function), args); // concerned
with control on Main Form
else
return;

Thread.Sleep(100); // Updates a control every 100 ms
}

if (!terminatePopulate)
this.Invoke(new MyOtherDelegate(Function), args);
}

private void OnClosing(object sender, EventArgs ev)
{
terminatePopulate = true;
}

}

Nov 13 '07 #1
5 5753
The problem here (aside from the synchronization issues that creep up in
setting terminatePopulate) is that in between the check on the value, the
form can be disposed of, which is causing your error.

Rather, what you should do is check the terminatePopulate value in the
delegate which gets called on the UI thread. This will cause you to not
access the form if the value is set.

Then, you can check the value in the loop on the thread in every
iteration to see if you should exit the thread.

You need to place a lock statement around access to the
terminatePopulate variable, as you need to synchrnoize access between the
threads.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<ai********@yahoo.comwrote in message
news:11**********************@v3g2000hsg.googlegro ups.com...
Hi,

The problem is that the line -
this.Invoke(new MyDelegate(Function), args); // this = MainForm
is being called after the line -
terminatePopulate = true;

therefore I get an exception which states that MainForm has been
disposed when this.Invoke is called...

Is there a way to check if MainForm has been disposed. Perhaps theres
a better way of doing what am trying to achieve,

Thanks,

Aine

public class FormMain : Form
{
volatile bool terminatePopulate = false;

void FormMain_Load()
{

myThread = new System.Threading.Thread(MyThread());
myThread.Start();
}

void MyThread()
{

while (!anotherObject.ContinueWaiting)
{

if (!terminatePopulate)
this.Invoke(new MyDelegate(Function), args); // concerned
with control on Main Form
else
return;

Thread.Sleep(100); // Updates a control every 100 ms
}

if (!terminatePopulate)
this.Invoke(new MyOtherDelegate(Function), args);
}

private void OnClosing(object sender, EventArgs ev)
{
terminatePopulate = true;
}

}

Nov 13 '07 #2
I would have thought it would be better to terminate the Thread when
the form is disposed. There is a Disposing event for this I believe.
That way the thread can never be running after the form has been
disposed. It also holds better to the IDisposable interface where the
Dispose function is supposed to clean up all resources used by the
object.

I would also recommend that you look into using events to terminate
the thread correctly. You have no way at the moment of knowing when
the thread terminates and so can end up with a situation where the
object is completely destroyed and usless but the thread is still
running. You should at least have myThread.Join() in the closing
event.


Nov 13 '07 #3
<ai********@yahoo.comwrote in message
news:11**********************@v3g2000hsg.googlegro ups.com...
Hi,

The problem is that the line -
this.Invoke(new MyDelegate(Function), args); // this = MainForm
is being called after the line -
terminatePopulate = true;

therefore I get an exception which states that MainForm has been
disposed when this.Invoke is called...

Is there a way to check if MainForm has been disposed. Perhaps theres
a better way of doing what am trying to achieve,

Thanks,

Aine

public class FormMain : Form
{
volatile bool terminatePopulate = false;

void FormMain_Load()
{

myThread = new System.Threading.Thread(MyThread());
myThread.Start();
}

void MyThread()
{

while (!anotherObject.ContinueWaiting)
{

if (!terminatePopulate)
this.Invoke(new MyDelegate(Function), args); // concerned
with control on Main Form
else
return;

Thread.Sleep(100); // Updates a control every 100 ms
}

if (!terminatePopulate)
this.Invoke(new MyOtherDelegate(Function), args);
}

private void OnClosing(object sender, EventArgs ev)
{
terminatePopulate = true;
}

}


Any reason why you are using Invoke instead of BeginInvoke?
Use BeginInvoke and set the flag in the FormClosing event handler.

Willy.
Nov 13 '07 #4
BeginInvoke will not change anything as it will just hide the
exception that still gets generated because the form has been
disposed.
Nov 14 '07 #5
"Olie" <ow****@gmail.comwrote in message
news:11**********************@v2g2000hsf.googlegro ups.com...
BeginInvoke will not change anything as it will just hide the
exception that still gets generated because the form has been
disposed.


Invoke has Synchronous call semantics, that is it waits until the delegated
callback returns, the caller is *assumed* to handle the return value and the
exception thrown by the callback .
BeginInvoke has "fire and forget" semantics, that means that the caller does
not care about whatever the delegate returns. In the OP's scenario, all he
needs is an event that indicates that he can stop updating the UI.
If he would like to get the result of the delegate (which can only be an
exception here), he would have wrapped the UI call in a try\catch block
anyway.
There is no easy way to prevent this race. There is no way to control the
sequence of the messages as stored in the Windows Message Queue. WM_CLOSE
message (queued by SendMessage) are always handled before Posted message (as
used by Invoke/BeginIvoke), that means that the already queued
"ThreadCallBack Messages" will be postponed till after the WM_CLOSE has been
processed and as such after the Control has been disposed. Whenever the
Windows loop picks up the previously queued "ThreadCallBack" message and
starts executing the callback it will get a "ObjectDisposedException"
thrown on him, all you can do here is catch the exception in the delegated
function.

Willy.

Nov 14 '07 #6

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

Similar topics

1
by: Eric Cadwell | last post by:
We're running Load Runner to test a large 1.0 based WinForms application. I'm getting the following exception when opening and closing a form 400 - 450 times. The form contains several controls and...
1
by: Rafael | last post by:
Hi, I hope I can find some help for this problem IDE: Visual Studio.NET 2003 Developer Editio Language: C# Problem: "An unhandled exception of type 'System.NullReferenceException' occurred in...
7
by: Ryan Park | last post by:
Hi, //SITUATION I got a panel control that hold a certain position on a form. Every controls or UIs are on this panel. At certain situation, I called dispose() method of this panel control...
5
by: mthgk | last post by:
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...
1
by: bw | last post by:
I have a basic custom collection that throws a new exception if the item(key) is not found in the collection. This is used as part of a calling function. It all works correctly, the problem...
8
by: bole2cant | last post by:
When I add a Button to my NotifyIcon program I get the following: An unhandled exception of type 'System.NullReferenceException' occurred in unknown module. Additional information: Object...
7
by: Boni | last post by:
Dear all, if I do myform.close() is it disposed or not? is it correct do following? myForm.close() Info=SomeControlOntheForm.text This seems to work. But am not sure that it is waterproof....
5
by: pcnerd | last post by:
I'm trying to create a program that plots randomly colored pixels on a bitmap & then displays the bitmap. When I run the program, I see the pixels being plotted down the left side of the form. When...
6
by: ahmad.humyn | last post by:
I want to call a hidden form. My code goes something like in which the main calls form1. form1 has a button which creates & calls form2 and hides itself. Now I have a button in form2 which if...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...

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.