URGENT: Problem with Multi Threading

I am running the following code:

//this.BeginInvoke(m_DelegateReload, null);
ThreadStart start = new ThreadStart(ReloadDataHelper);
Thread t = new Thread(start);
t.IsBackground = true;
t.Start();

But, it is not dying when the calling thread, the main GUI, exits.  I am calling Application.Exit() and then getting an error from the ReloadDataHelper because it is trying to populate a list view that does not exist.  can someone help?  
LVL 5
jjacksnAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Balder1978Commented:
I dont understand exactly, but why can't you use:

ThreadStart start = new ThreadStart(ReloadDataHelper);
start.IsBackground = true;
start.Start();
0
Balder1978Commented:
Sorry, didn't read it all...
private Thread t;

t = new Thread(new ThreadStart(ReloadDataHelper));
t.IsBackground = true;
t.Start();

in the Form_Closing event:
if (t != null && t.IsAlive) t.Abort();

Hope this helps.
0
smeggheadCommented:
Are there any other threads that are foreground which might still be running ?? your original code looks like it should work in the way you want

from MSDN.

"a background thread will not keep the managed execution environment alive. Once all foreground threads have been stopped in a managed process (where the .exe file is a managed assembly), the system stops all background threads and shuts down."

Is the exe a managed assembly??

Smg.
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

jjacksnAuthor Commented:
I don't think so.  Every thread I start in my program is run as a background thread.  There are multiple background threads running...

These is a different question, but maybe you can help here.  Whenever I go to file->exit my application closes.  But sometime, the X button doesn't work.  Why would it sometimes not work to close the application?  
0
Balder1978Commented:
Because some theard is still running,  At least that is what happend to me...
0
jjacksnAuthor Commented:
is there a way to force the X button to Call Application.Exit()?  How can I add a handler to it.  Adding to close or closing does not work, obviously, since the form is not closing when that button is clicked.
0
smeggheadCommented:
I had this problem a while back, and it was due to a timer on the form which was still active.
0
jjacksnAuthor Commented:
I do have background threads running, I would just like them to die (I"m mean).  So how can I add a handler to the X button?
0
Balder1978Commented:
The forms Closing event!
Check if the threads are running or not with Thread.IsAlive, so if it is alive, then do a Thread.Abort() to terminate the thread
0
AvonWyssCommented:
Your threads will die. The problem seems more to be that the thread is trying to access some GUI component (which a thread should not do anyways) and this leads to an exception...
0
jrbergCommented:
Create a global ArrayList in your main class and every time you create a thread store a reference to it in the ArrayList.  Then inside of your protected override void dispose() method loop the ArrayList and kill the threads if they are still active.

Hope this helps,
Jerry

1) ArrayList threadArrayList; // Create this globally

2) threadArrayList.Add( thread ); // Do this every time you create a thread

3) Do this in your dispose method
for( int i=0; i<threadArrayList.Count; i++ )
{
  if(((Thread)threadArrayList[i]).IsAlive)
  {
     ((Thread)threadArrayList[i]).Abort();  
  }
}

Hope this idea gives you what you need,
Jerry
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AvonWyssCommented:
Note: you'd need to synchronize the ArrayList or you may get strange results...
0
jrbergCommented:
I've never had any problems.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.