How to test a thread handle is valid handle?
-
I have a thread handle,when the thread exit.the handle is invalid(0xfeeefeee). and i want to clearly when is it valid and when is it invalid. please show me a solution,thanks. Scratch
A Thread handle IS valid even after the Thread exits. There is an error in your code that's corrupting the handle value. But, answering your question: you can always call a harmless API function that needs a thread handle, say GetThreadPriority. If it returns ERROR_INVALID_HANDLE, you know the handle is invalid. lazy isn't my middle name.. its my first.. people just keep calling me Mel cause that's what they put on my drivers license. - Mel Feik
-
A Thread handle IS valid even after the Thread exits. There is an error in your code that's corrupting the handle value. But, answering your question: you can always call a harmless API function that needs a thread handle, say GetThreadPriority. If it returns ERROR_INVALID_HANDLE, you know the handle is invalid. lazy isn't my middle name.. its my first.. people just keep calling me Mel cause that's what they put on my drivers license. - Mel Feik
thank you for your help,but my code goes like: In main thread.
if(m_pScanThread && m_pScanThread->m_hThread != NULL) { m_pScanThread->PostThreadMessage(UWM_TIMERON,0,0); } else { m_pScanThread = AfxBeginThread((AFX_THREADPROC)fnScanProc,NULL); if(m_pScanThread && m_pScanThread->m_hThread != NULL) m_pScanThread->PostThreadMessage(UWM_TIMERON,0,0); }
Thread function.CoInitializeEx(NULL,COINIT_MULTITHREADED ); try { //Do some Initialize DWORD dwRet; MSG msg; while(dwRet = GetMessage(&msg,NULL,0,0) != 0) { if(dwRet == -1) { continue; } else { if(msg.message == UWM_TIMERON) Scan(...); else DispatchMessage(&msg); } } } catch(...) { return -1;**///Here! if return then the m_pScanThread's( member of the main ///thread)member m_hThread is 0xfeeefeee ///and at this time i want to re - AfxBeginThread...** ::CoUninitialize(); } ::CoUninitialize(); return 0;
Scratch -
thank you for your help,but my code goes like: In main thread.
if(m_pScanThread && m_pScanThread->m_hThread != NULL) { m_pScanThread->PostThreadMessage(UWM_TIMERON,0,0); } else { m_pScanThread = AfxBeginThread((AFX_THREADPROC)fnScanProc,NULL); if(m_pScanThread && m_pScanThread->m_hThread != NULL) m_pScanThread->PostThreadMessage(UWM_TIMERON,0,0); }
Thread function.CoInitializeEx(NULL,COINIT_MULTITHREADED ); try { //Do some Initialize DWORD dwRet; MSG msg; while(dwRet = GetMessage(&msg,NULL,0,0) != 0) { if(dwRet == -1) { continue; } else { if(msg.message == UWM_TIMERON) Scan(...); else DispatchMessage(&msg); } } } catch(...) { return -1;**///Here! if return then the m_pScanThread's( member of the main ///thread)member m_hThread is 0xfeeefeee ///and at this time i want to re - AfxBeginThread...** ::CoUninitialize(); } ::CoUninitialize(); return 0;
ScratchI never use MFC thread classes, for me they are just so much trouble for a simple _beginthread. But I'm sure that you can store the m_hThread in another variable just before the thread starts. and, beware of this code:
m_pScanThread = AfxBeginThread((AFX_THREADPROC)fnScanProc,NULL);
if(m_pScanThread && m_pScanThread->m_hThread != NULL)
m_pScanThread->PostThreadMessage(UWM_TIMERON,0,0);IIRC, you need a small Sleep or a Sleep(0) just after the AfxBeginThread for the other thread begin starting, otherwise in a 100% CPU machine you'll have problems. lazy isn't my middle name.. its my first.. people just keep calling me Mel cause that's what they put on my drivers license. - Mel Feik
-
I never use MFC thread classes, for me they are just so much trouble for a simple _beginthread. But I'm sure that you can store the m_hThread in another variable just before the thread starts. and, beware of this code:
m_pScanThread = AfxBeginThread((AFX_THREADPROC)fnScanProc,NULL);
if(m_pScanThread && m_pScanThread->m_hThread != NULL)
m_pScanThread->PostThreadMessage(UWM_TIMERON,0,0);IIRC, you need a small Sleep or a Sleep(0) just after the AfxBeginThread for the other thread begin starting, otherwise in a 100% CPU machine you'll have problems. lazy isn't my middle name.. its my first.. people just keep calling me Mel cause that's what they put on my drivers license. - Mel Feik
Be warned! Using ::BeginThread with mfc corupts the mfc state handling! As far as I understand _beginthread is a wrapper for the Win32 API.