Click here to Skip to main content
16,005,236 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
Generalimage processing Pin
giti31-May-04 11:18
giti31-May-04 11:18 
Generalmultiple RPCs in one process. Pin
leonmccalla@hotmail.com31-May-04 9:36
leonmccalla@hotmail.com31-May-04 9:36 
GeneralThread Handle Query Pin
Nirav Doshi31-May-04 9:30
Nirav Doshi31-May-04 9:30 
GeneralRe: Thread Handle Query Pin
Michael Dunn31-May-04 12:26
sitebuilderMichael Dunn31-May-04 12:26 
GeneralRe: Thread Handle Query Pin
Nirav Doshi31-May-04 20:39
Nirav Doshi31-May-04 20:39 
GeneralRe: Thread Handle Query Pin
Diddy1-Jun-04 1:54
Diddy1-Jun-04 1:54 
GeneralRe: Thread Handle Query Pin
Nirav Doshi1-Jun-04 2:13
Nirav Doshi1-Jun-04 2:13 
GeneralRe: Thread Handle Query Pin
Diddy1-Jun-04 5:10
Diddy1-Jun-04 5:10 
To check if a thread is running:

CWinThread* pThread = ....

if(::WaitForSingleObject(pThread->m_hThread, 0) == WAIT_OBJECT_0))
{
// thread is running
}
else
{
// thread has finished
}

The other way is also:

if(::GetExitCodeThread(pThread->m_hThread, &dwExit) != 0 && dwExit == STILL_ACTIVE)
{
// thread is running
}
else
{
// thread has finished
}

But the first is usually prefered.

With both, you must be careful you are working with a valid thread handle. FYI there are 2 ways (that i know of) to be sure you have a vaid handle with MFC threads:

Autodelete - default
---------------------
CWinThread* pThread = ::AfxBeginThread(...);

This way, as you have discovered, you can't do anything with the pThread as MFC can delete it at any time. After the above thread has been started, doing pThread-> is ALWAYS a risky bit of code. You don't know when MFC will kill off your CWinThread object, and you could be refering to a bit of memory that has just been deleted.

DuplicateHandle
----------------
CWinThread* pThread = ::AfxBeginThread(...CREATE_SUSPENDED);
HANDLE h;
::DuplicateHandle(pThread->m_hThread, h, ...);

This works because handles are refrenced counted. The DuplicateHanld call bumps this count up to 2 and puts a copy in 'h', so when CWinThread autodeletes and does a CloseHandle on it's m_hThread member, the handle isn't realy closed until you do a CloseHandle on 'h'. Don't be tempted to do this:

CWinThread* pThread = ::AfxBeginThread(...CREATE_SUSPENDED);
HANDLE h = pThread->m_hThread;

As your count will still only be at 1.

m_bAutoDelete = FALSE;
----------------------
As shown above, this is usually the easiest. Just delete your CWinThread* and that will close your thread handle.
GeneralRe: Thread Handle Query Pin
Nirav Doshi1-Jun-04 8:03
Nirav Doshi1-Jun-04 8:03 
GeneralRe: Thread Handle Query Pin
Diddy1-Jun-04 10:27
Diddy1-Jun-04 10:27 
GeneralCostom Draw Problem!!! Pin
VirgoCI31-May-04 8:24
VirgoCI31-May-04 8:24 
GeneralRe: Costom Draw Problem!!! Pin
Antti Keskinen31-May-04 11:46
Antti Keskinen31-May-04 11:46 
GeneralRe: Costom Draw Problem!!! Pin
VirgoCI31-May-04 17:48
VirgoCI31-May-04 17:48 
Generalsmtp question Pin
includeh1031-May-04 8:16
includeh1031-May-04 8:16 
GeneralRe: smtp question Pin
bryce31-May-04 15:10
bryce31-May-04 15:10 
GeneralProblem with SetTextColor Pin
Aviv Halperin31-May-04 7:50
Aviv Halperin31-May-04 7:50 
GeneralRe: Problem with SetTextColor Pin
Brian D31-May-04 8:15
Brian D31-May-04 8:15 
GeneralRe: Problem with SetTextColor Pin
includeh1031-May-04 8:21
includeh1031-May-04 8:21 
GeneralRe: Problem with SetTextColor Pin
f6431-May-04 8:22
f6431-May-04 8:22 
GeneralRe: Problem with SetTextColor Pin
Aviv Halperin31-May-04 9:16
Aviv Halperin31-May-04 9:16 
Generaldocumentation of jpeg format Pin
includeh1031-May-04 6:54
includeh1031-May-04 6:54 
GeneralRe: documentation of jpeg format Pin
Nirav Doshi31-May-04 9:11
Nirav Doshi31-May-04 9:11 
GeneralRe: documentation of jpeg format Pin
includeh1031-May-04 9:49
includeh1031-May-04 9:49 
GeneralRe: documentation of jpeg format Pin
Nirav Doshi31-May-04 22:23
Nirav Doshi31-May-04 22:23 
GeneralGradients Pin
Archer28231-May-04 6:35
Archer28231-May-04 6:35 

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.