Click here to Skip to main content
15,885,186 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: How to stop the thread if it's running? Pin
Rajesh R Subramanian29-Apr-09 23:11
professionalRajesh R Subramanian29-Apr-09 23:11 
GeneralRe: How to stop the thread if it's running? Pin
CPallini29-Apr-09 23:34
mveCPallini29-Apr-09 23:34 
GeneralRe: How to stop the thread if it's running? Pin
CPallini29-Apr-09 23:32
mveCPallini29-Apr-09 23:32 
GeneralRe: How to stop the thread if it's running? Pin
Roger Stoltz29-Apr-09 23:40
Roger Stoltz29-Apr-09 23:40 
GeneralRe: How to stop the thread if it's running? Pin
CPallini29-Apr-09 23:44
mveCPallini29-Apr-09 23:44 
GeneralRe: How to stop the thread if it's running? Pin
Rajesh R Subramanian29-Apr-09 22:45
professionalRajesh R Subramanian29-Apr-09 22:45 
GeneralRe: How to stop the thread if it's running? Pin
mikert_200830-Apr-09 0:26
mikert_200830-Apr-09 0:26 
GeneralRe: How to stop the thread if it's running? Pin
Roger Stoltz30-Apr-09 2:54
Roger Stoltz30-Apr-09 2:54 
mikert_2008 wrote:
CWinThread * myWorkerThread;
myWorkerThread = AfxBeginThread(run, this);

myWorkerThread->m_bAutoDelete = TRUE;



In all the confusion from the discussion earlier today you're partly forgiven, but consider the following for spawning the worker thread:
CWinThread* pThread;
// Create thread suspended to avoid race conditions
pThread = AfxBeginThread( run, this, 0, 0, CREATE_SUSPENDED, NULL );

// Make sure the thread could be created and  
// the pointer is valid before operating on it
if( pThread )
{
    // Don't let the object delete itself since we want 
    // to be able to wait on the thread handle
    // This is important and addressed in the article linked to by Carlo!
    pThread->m_bAutodelete = FALSE;
    pThread->ResumeThread();
}

What kind of event stops the worker thread from running?
The way I interpret your destructor is that the thread does its job and then terminates by itself.
What does the thread do? Your comment "display the data" worries me since you should not touch the GUI from a worker thread in order to avoid deadlocks.
Probably you shouldn't be using multithreading at all for the problem you're trying to solve, since you seem to be doing thing sequentially and not in parallel.

Your destructor doesn't really make sense. There's also a race condition that in most cases would generate an exception when you're trying to free memory that has already been freed since you haven't set m_bAutoDelete to FALSE. You're mixing manual and automatic deletion of the CWinThread object.

You should also make sure all thread spawned by your program are terminated before allowing the program to exit. It's considered Best Practice because when you exit your program you don't have the power to do anything about it any longer.
The most harsh way the code that waits for the thread to exit could be like this:
if( pThread )
{
    // Here should probably be something that tells the thread 
    // to stop executing such as an event that is signalled

    if( ::WaitForSingleObject( pThread->m_hThread, INFINITE ) == WAIT_OBJECT_0 )
    {
        // Now it's safe to delete the CWinThread object!
        delete pThread;
        pThread = NULL;
    }
}


You need to get back to the article and read it again as the above suggests that you haven't read it carefully enough and apparently have not understood it properly.
Believe me, I've suggested Joe's article to fellow programmers since he wrote it almost a decade ago. In my opinion it's the best article that can be found on the internet describing multithreading, especially together with MFC framework.


"It's supposed to be hard, otherwise anybody could do it!" - selfquote
"High speed never compensates for wrong direction!" - unknown


QuestionRe: How to stop the thread if it's running? Pin
David Crow30-Apr-09 3:58
David Crow30-Apr-09 3:58 
AnswerRe: How to stop the thread if it's running? Pin
David Crow30-Apr-09 4:07
David Crow30-Apr-09 4:07 
QuestionHow can I use C# Managed DLL into VC++ Application Pin
PankajB29-Apr-09 20:43
PankajB29-Apr-09 20:43 
AnswerRe: How can I use C# Managed DLL into VC++ Application Pin
Stuart Dootson29-Apr-09 20:50
professionalStuart Dootson29-Apr-09 20:50 
GeneralRe: How can I use C# Managed DLL into VC++ Application Pin
PankajB29-Apr-09 20:53
PankajB29-Apr-09 20:53 
Questionbest font for hindi Pin
Purish Dwivedi29-Apr-09 20:15
Purish Dwivedi29-Apr-09 20:15 
AnswerRe: best font for hindi Pin
CPallini29-Apr-09 21:09
mveCPallini29-Apr-09 21:09 
GeneralRe: best font for hindi Pin
Purish Dwivedi29-Apr-09 22:00
Purish Dwivedi29-Apr-09 22:00 
AnswerRe: best font for hindi Pin
Michael Schubert29-Apr-09 22:05
Michael Schubert29-Apr-09 22:05 
AnswerRe: best font for hindi Pin
Rajesh R Subramanian29-Apr-09 22:15
professionalRajesh R Subramanian29-Apr-09 22:15 
QuestionVisualC++ LineTo() call causes high usage of resouce, causing more RAM usage - Memory Not Releasing Pin
adepumadhu129-Apr-09 20:15
adepumadhu129-Apr-09 20:15 
AnswerRe: VisualC++ LineTo() call causes high usage of resouce, causing more RAM usage - Memory Not Releasing Pin
Cedric Moonen29-Apr-09 20:26
Cedric Moonen29-Apr-09 20:26 
AnswerRe: VisualC++ LineTo() call causes high usage of resouce, causing more RAM usage - Memory Not Releasing Pin
Perry Holman29-Apr-09 22:12
Perry Holman29-Apr-09 22:12 
QuestionRe: VisualC++ LineTo() call causes high usage of resouce, causing more RAM usage - Memory Not Releasing Pin
David Crow30-Apr-09 4:12
David Crow30-Apr-09 4:12 
QuestionNeed help on finishing this basic C program [modified] Pin
biggiant2200029-Apr-09 19:35
biggiant2200029-Apr-09 19:35 
AnswerRe: Need help on finishing this basic C program Pin
_AnsHUMAN_ 29-Apr-09 19:57
_AnsHUMAN_ 29-Apr-09 19:57 
GeneralRe: Need help on finishing this basic C program Pin
biggiant220001-May-09 6:29
biggiant220001-May-09 6:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.