?
Solved

Terminating A Thread Externally

Posted on 2006-07-24
8
Medium Priority
?
423 Views
Last Modified: 2013-11-20
I have an MFC dialog app that shows live updates on information coming into a database.  It does this by invoking a thread from OnInitDialog using AfxBeginThread.  This thread then goes round in a continuous loop, reading the database info and then outputting it to the screen.  The problem I am having is how to close this thread in a controlled way when my main application stops running.  I need the chance to return my dynamic memory to avoid debug assertion fail messages on shutdown, and ensure that when I write my data to the screen it will not cause my app to crash because the main dialog has been wrapped up.  AfxEndThread can only be called from inside the thread, there a way of terminating the thread from the main dialog app?
0
Comment
Question by:reidy_boy
[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
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 22

Accepted Solution

by:
mahesh1402 earned 500 total points
ID: 17166647
You need to arrange to signal to the thread, and then have the thread exit itself - safely. One way of doing this is to create an event and have the thread periodically check this event.  If the threads are using WaitForMultipleObject calls pass them an event that you set to tell them to shutdown. WaitForMultipleObject will return and its return code can cause the thread code to exit.

Refer : Using Worker Threads by  Joseph M Newcomer  : http://www.codeproject.com/threads/usingworkerthreads.asp ( ref : Pausing a Thread and Thread Shutdown  )

-MAHESH
0
 
LVL 7

Expert Comment

by:nafis_devlpr
ID: 17174128
just use a Global variable in the while loop condition to wether stop or go on with the threads.
like this

//Global Variable
int stopThread=0;//thread count starts from 1

in the thread functions parameter pass the thread number, this is because if you have multiple threads running at the same time then you have to signal the right Thread to stop. So when you need to stop the thread, in the main function do this:

//in main function
stopThread=2;//terminates the 2nd thread

//in the thread
//the while condition
while(stopThread != thisThread)//thisThread is the id of this threads passed from main through AfxBeginThread
//function
{
................................
}

AfxEndThread(exitCode);

hope this helps

Nafis
0
 
LVL 1

Author Comment

by:reidy_boy
ID: 17174738
I am now using WaitForSingleObject.  This works fine but with one problem.  Whenever I signal that I want my thread to end, the thread will continue running as it should until it reaches a line that is associated with the View class.  Two such lines are GetCurSel() and ResetContent().  When it tries to execute these lines the program freezes.
0
Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

 
LVL 7

Expert Comment

by:nafis_devlpr
ID: 17175045
the program is freezing because you are exiting from the main function before the thread ends. You have to wait until the thread ends then exit from the main function. you can do this using a static thread count. whenever a thread ends before it ends it should decrease the thread count, so when the thread count becomes zero the main will exit.

Nafis
0
 
LVL 1

Author Comment

by:reidy_boy
ID: 17175373
I don't see how I can be exiting from the main function first.  The setup is this.  I have a boolean variable declared in my Dlg header file called KillThread.  In the dialog constructor I set this to 0.  I have a Close button on my main dialog window, and an OnClose method which is called when this button is pressed.  The code in this function reads as follows:

KillThread = 1;
myWorkerThread->m_bAutoDelete = FALSE;
WaitForSingleObject(myWorkerThread->m_hThread, INFINITE);
delete myWorkerThread;
CDialog::OnClose();

So it is only after WaitForSingleObject has been executed (i.e. once my thread has been successfully ended) that it destroys the main window.
0
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 300 total points
ID: 17176150
<Whenever I signal that I want my thread to end, the thread will continue running as it should until it reaches a line that is associated with the View class.>

You are aware that MFC is not thread safe I hope.  (Calling an MFC window directly from another thread is a nono).
0
 
LVL 1

Author Comment

by:reidy_boy
ID: 17176590
> You are aware that MFC is not thread safe I hope.
Er... no :-)

>Calling an MFC window directly from another thread is a >nono

What would be your approach to this task?  Would it be better for the thread to post a windows message that the View frame would then listen out for?
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 17176695
<Would it be better for the thread to post a windows message that the View frame would then listen out for?>

Yes, you should use a technique such as that.   (From another thread you can call some MFC window functions directly, other will crash because it wants some thread specific info.)


ps.
Did you consider a simple 'DoEvents' function - it can make things appear multi-threaded even if they aren't.  What you have described sounds suspiciously like a case for that.
0

Featured Post

Get MySQL database support online, now!

At Percona’s web store you can order your MySQL database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card.

Question has a verified solution.

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

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

777 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