Solved

URGENT: Problem with Multi Threading

Posted on 2003-11-30
13
346 Views
Last Modified: 2010-05-19
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?  
0
Comment
Question by:jjacksn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +2
13 Comments
 
LVL 2

Expert Comment

by:Balder1978
ID: 9847097
I dont understand exactly, but why can't you use:

ThreadStart start = new ThreadStart(ReloadDataHelper);
start.IsBackground = true;
start.Start();
0
 
LVL 2

Expert Comment

by:Balder1978
ID: 9847104
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
 
LVL 10

Expert Comment

by:smegghead
ID: 9847137
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 5

Author Comment

by:jjacksn
ID: 9847686
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
 
LVL 2

Expert Comment

by:Balder1978
ID: 9848456
Because some theard is still running,  At least that is what happend to me...
0
 
LVL 5

Author Comment

by:jjacksn
ID: 9848711
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
 
LVL 10

Expert Comment

by:smegghead
ID: 9849571
I had this problem a while back, and it was due to a timer on the form which was still active.
0
 
LVL 5

Author Comment

by:jjacksn
ID: 9851231
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
 
LVL 2

Expert Comment

by:Balder1978
ID: 9851319
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
 
LVL 14

Expert Comment

by:AvonWyss
ID: 10024867
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
 
LVL 2

Accepted Solution

by:
jrberg earned 500 total points
ID: 10110539
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
 
LVL 14

Expert Comment

by:AvonWyss
ID: 10122608
Note: you'd need to synchronize the ArrayList or you may get strange results...
0
 
LVL 2

Expert Comment

by:jrberg
ID: 10193559
I've never had any problems.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
linq, c# 8 52
How to use spatial data types in SQL Server Database project 2 66
array not updating 8 42
C# Web service insert into Oracle table 8 43
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question