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

Form Object Disposal Checking From Thread

Hello,

Ok I have 2 classes in my project, one is the main form and one is a
connection class, at a certain event on my main form a new instance is
made of the connection class, and a reference to the main form is passed
to its constructor.

The connection class opens up a new thread and starts doing work in it,
and adds collected data to the main form via a (cross-thread) control
invoking delegate.

The problem arises if the main form is closed while the thread is doing
work which involves using the delegate and adding data to the controls
on the main form. Basically i get an exception stating the main form has
been disposed and that the thread cannot access it.

So to prevent this, I process the main forms _FormClosing event, my code
in this event sets a boolean termination flag in the connection class to
true.
The idea being that the thread checks this flag to see if it should
terminate.

The problem is the thread is involved in a loop during the point of
closure and it dosnt catch the updated termination flag until the form
has been disposed by which time its too late.

The thread code thats busy looks like this:
Code:

do
{
if (terminate == true)
return -1;

else if (group_info != "-end-")
{
group_info = CSReader.ReadLine();
mform.Debug_Console_Add_Item(group_info);
}

} while (group_info != ".");
mform, is the reference to the main form, and this is the line that
throws the exception that the objects been disposed. If i add a
Thread.Sleep(2000) line into the top of the else if then it works, as it
catches the flag and returns with -1 as i want and avoids the exception.

So how can i prevent this? i.e Ideally I need the form to wait for a
signal from the thread saying its terminated before disposing, but if do
Thread.CurrentThread.Join() in the form closing method, it practically
puts it in an infinite loop and the line
mform.Debug_Console_Add_Item(group_info); cannot execute... So nothing
happens.

Anyone faced this problem before and know how to get around it? I need
to terminate the thread cleanly so my connection code can tidy up.

I know i could just call thread.abort() and process the exception, but
its not a very clean way of doing it.

Ive also tried checking the forms closing status inside the control
delegate, but im not having any luck as its either too soon or too late
Crying or Very sad

Thanks,
Mike

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
Apr 7 '07 #1
2 3716
I would suggest you create events that the background thread would raise to
any subscriber (in this case, the Form). When the form closes it would
unsubscribe and the background thread need not care...

Otherwise, you might want to consider using a BackgroundWorker class, which
would accomplish the same thing (where the progress event would be subscribed
to by the Form and the background thread (the DoWork handler) would send data
via the event to the subscriber.
--
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#
"Mike" wrote:
Hello,

Ok I have 2 classes in my project, one is the main form and one is a
connection class, at a certain event on my main form a new instance is
made of the connection class, and a reference to the main form is passed
to its constructor.

The connection class opens up a new thread and starts doing work in it,
and adds collected data to the main form via a (cross-thread) control
invoking delegate.

The problem arises if the main form is closed while the thread is doing
work which involves using the delegate and adding data to the controls
on the main form. Basically i get an exception stating the main form has
been disposed and that the thread cannot access it.

So to prevent this, I process the main forms _FormClosing event, my code
in this event sets a boolean termination flag in the connection class to
true.
The idea being that the thread checks this flag to see if it should
terminate.

The problem is the thread is involved in a loop during the point of
closure and it dosnt catch the updated termination flag until the form
has been disposed by which time its too late.

The thread code thats busy looks like this:
Code:

do
{
if (terminate == true)
return -1;

else if (group_info != "-end-")
{
group_info = CSReader.ReadLine();
mform.Debug_Console_Add_Item(group_info);
}

} while (group_info != ".");
mform, is the reference to the main form, and this is the line that
throws the exception that the objects been disposed. If i add a
Thread.Sleep(2000) line into the top of the else if then it works, as it
catches the flag and returns with -1 as i want and avoids the exception.

So how can i prevent this? i.e Ideally I need the form to wait for a
signal from the thread saying its terminated before disposing, but if do
Thread.CurrentThread.Join() in the form closing method, it practically
puts it in an infinite loop and the line
mform.Debug_Console_Add_Item(group_info); cannot execute... So nothing
happens.

Anyone faced this problem before and know how to get around it? I need
to terminate the thread cleanly so my connection code can tidy up.

I know i could just call thread.abort() and process the exception, but
its not a very clean way of doing it.

Ive also tried checking the forms closing status inside the control
delegate, but im not having any luck as its either too soon or too late
Crying or Very sad

Thanks,
Mike

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
Apr 8 '07 #2
Mike,

This appears to be more of a synchronization issue.

You are depending over the writes on a global variable to halt or
resume process of a secondary thread. While this way of processing in
general is common in multi threading, one however must be aware that
between reads an update can occur, by the time you read a global
variable and determined the processing logic chances are that another
thread may have written a value to the global variable.

A probable solution would be to consider double checking the
synchronizer (as below) and also ensure that form is not disposed off.
The synchronizer per your example is "terminate" global variable

do
{
if (terminate == true) //Initial check
return -1;
else if (group_info != "-end-")
{
group_info = CSReader.ReadLine();
///double check another thread by now
has not updated the "terminate" variable
///and form is not dispsoed
if ((!terminate) && (!mform.Isdipsosed)) //verify again
mform.Debug_Console_Add_Item(group_info);
}
} while (group_info != ".");

I hope this helps

Raaj
On Apr 7, 5:37 pm, Mike <Mikelaw...@powerpound.netwrote:
Hello,

Ok I have 2 classes in my project, one is the main form and one is a
connection class, at a certain event on my main form a new instance is
made of the connection class, and a reference to the main form is passed
to its constructor.

The connection class opens up a new thread and starts doing work in it,
and adds collected data to the main form via a (cross-thread) control
invoking delegate.

The problem arises if the main form is closed while the thread is doing
work which involves using the delegate and adding data to the controls
on the main form. Basically i get an exception stating the main form has
been disposed and that the thread cannot access it.

So to prevent this, I process the main forms _FormClosing event, my code
in this event sets a boolean termination flag in the connection class to
true.
The idea being that the thread checks this flag to see if it should
terminate.

The problem is the thread is involved in a loop during the point of
closure and it dosnt catch the updated termination flag until the form
has been disposed by which time its too late.

The thread code thats busy looks like this:
Code:

do
{
if (terminate == true)
return -1;

else if (group_info != "-end-")
{
group_info = CSReader.ReadLine();
mform.Debug_Console_Add_Item(group_info);
}

} while (group_info != ".");

mform, is the reference to the main form, and this is the line that
throws the exception that the objects been disposed. If i add a
Thread.Sleep(2000) line into the top of the else if then it works, as it
catches the flag and returns with -1 as i want and avoids the exception.

So how can i prevent this? i.e Ideally I need the form to wait for a
signal from the thread saying its terminated before disposing, but if do
Thread.CurrentThread.Join() in the form closing method, it practically
puts it in an infinite loop and the line
mform.Debug_Console_Add_Item(group_info); cannot execute... So nothing
happens.

Anyone faced this problem before and know how to get around it? I need
to terminate the thread cleanly so my connection code can tidy up.

I know i could just call thread.abort() and process the exception, but
its not a very clean way of doing it.

Ive also tried checking the forms closing status inside the control
delegate, but im not having any luck as its either too soon or too late
Crying or Very sad

Thanks,
Mike

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access

Apr 8 '07 #3

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

Similar topics

10
by: Drakier Dominaeus | last post by:
This is my first time posting here, so please forgive me if I do anything incorrectly. I've been learning C# and working with different things and decided I wanted to get into Multi-Threading....
5
by: Stephen Lamb | last post by:
I have a background worker thread which I start from a form's HandleCreated event that makes calls back to the form using Invoke. During shutdown the form is disposed and the background worker...
16
by: Java script Dude | last post by:
Creating a method of object (Object.prototype.classOf ...) is not the correct way because of a (as expected) flaw in IE where DOM Elements does not inherit from Object. As a result it is best to...
16
by: anonymous.user0 | last post by:
The way I understand it, if I have an object Listener that has registered as a listener for some event Event that's produced by an object Emitter, as long as Emitter is still allocated Listener...
19
by: Taras_96 | last post by:
Hi everyone, How do you detect that a form element has been changed? This thread: ...
5
by: Miro | last post by:
I will try my best to ask this question correctly. I think in the end the code will make more sence of what I am trying to accomplish. I am just not sure of what to search for on the net. I...
7
by: Steve | last post by:
I have a method that creates of new form each time its called do i need to dipose of the form when it closes or doe the garbage collector take care of it? code private void button1_Click(object...
32
by: =?Utf-8?B?U2l2?= | last post by:
I have a form that I programmatically generate some check boxes and labels on. Later on when I want to draw the form with different data I want to clear the previously created items and then put...
3
by: stumorgan | last post by:
Basically what I have is a form with a graph on it which graphs data that I'm reading from a USB device at 100 Hz (every 10ms). I have a thread reading and parsing the data from the USB, but when...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.