Click to See Complete Forum and Search --> : closing the handle
miteshpandey
January 2nd, 2005, 05:40 AM
I have a variable m_hThread in my thread class.
I create the thread like this:
m_hThread = AfxBeginThread(ThreadProc, this);
In the destructor of the class of which my thread class is a member I am doing the following.
SetEvent(m_hThreadKillEvent);
WaitForSingleObject(MyThreadObject->m_hThread, INFINITE);
CloseHandle(MyThreadObject->MyThreadObject);
I am getting error in the highlighted line. The error is invalid handle. I believe the problem is that the thread I created closes the handle.
How should I get over this problem? Or should I not close the handle.
The problem still occurs event if I use CWinThread derived class.
Please Help.
NoHero
January 2nd, 2005, 06:09 AM
How does your thread procedure look like? If you object was released depends on the call of AfxEndThread:
void AFXAPI AfxEndThread(
UINT nExitCode,
BOOL bDelete = TRUE
);
Parameters
nExitCode: Specifies the exit code of the thread.
bDelete: Deletes the thread object from memory.
And remakrt that the bDelete paramater is automatically set to TRUE.
miteshpandey
January 2nd, 2005, 10:02 AM
I do not use AfxEndThread to end the thread. The thread is closed when the ThreadProc function returns. I think AfxEndThread is called implicitly.
My ThreadProc is like this:
UINT CThreadClass::ThreadProc(LPVOID Param)
{
CThreadClass *pThis = (CThreadClass*)Param;
if(pThis)
{
while(WaitForSingleObject(pThis->m_hThreadKillEvent, 0) != WAIT_OBJECT_0)
{
Do some stuff.............
}
}
return 0;
}
NoHero
January 2nd, 2005, 10:10 AM
AfxBeginThread creates a new CWinThread object, calls its CreateThread function to start executing the thread, and returns a pointer to the thread. Checks are made throughout the procedure to make sure all objects are deallocated properly should any part of the creation fail. To end the thread, call AfxEndThread from within the thread, or return from the controlling function of the worker thread.
I think it cleans up the handle for you. Have you tested through the debugger which value the handle holds? But I think you should explicity call AfxEndThread to be shure this clean up will occur.
miteshpandey
January 2nd, 2005, 12:52 PM
Originally Posted by MSDN
AfxBeginThread creates a new CWinThread object, calls its CreateThread function to start executing the thread, and returns a pointer to the thread. Checks are made throughout the procedure to make sure all objects are deallocated properly should any part of the creation fail. To end the thread, call AfxEndThread from within the thread, or return from the controlling function of the worker thread.
I am returning from the ThreadProc function.
I read an article suggested to me by one of the code guru members and according to it we can use DuplicateHandle(...) function.
Is this the case to use DuplicateHandle(...)?
If yes how do I supply all the parameters needed by DuplicateHandle(...)?
When I executed the debugger, the m_hThread handle seems to have a genuine value but the value is invalid.
Is it alright that I do not close the handle since I believe that the handle is closed by the worker thread.
I don't have the use of the handle further more. But just wanted my variable m_hThread to not to hold invalid value so that it looks nice.
Please Help.
NoHero
January 2nd, 2005, 12:58 PM
Just use AfxEndThread() inside your thread procedure and everything is fine :)
HANDLE hProc = GetCurrentProcess();
HANLDE hClone;
DuplicateHandle(hProc, m_hThread, hProc, &hClone, 0, FALSE, DUPLICATE_SAME_ACCESS);
This should clone your handle and retrieve the clone in "hClone". Now you got two nice handles :)
miteshpandey
January 2nd, 2005, 01:34 PM
Thank you very much for your replies.
Now, I understand that I need to used AfxEndThread(...).
NoHero
January 2nd, 2005, 01:45 PM
Thank you very much for your replies.
Now, I understand that I need to used AfxEndThread(...).
You are very welcome!
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.