Solved

Terminating A Thread Externally

Posted on 2006-07-24
8
413 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
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 22

Accepted Solution

by:
mahesh1402 earned 125 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 7

Expert Comment

by:nafis_devlpr
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Author Comment

by:reidy_boy
Comment Utility
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 75 total points
Comment Utility
<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
Comment Utility
> 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
Comment Utility
<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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now