Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to test a thread handle is valid handle?

How to test a thread handle is valid handle?

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lizp
    wrote on last edited by
    #1

    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

    D 1 Reply Last reply
    0
    • L Lizp

      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

      D Offline
      D Offline
      Daniel Turini
      wrote on last edited by
      #2

      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

      L 1 Reply Last reply
      0
      • D Daniel Turini

        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

        L Offline
        L Offline
        Lizp
        wrote on last edited by
        #3

        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

        D 1 Reply Last reply
        0
        • L Lizp

          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

          D Offline
          D Offline
          Daniel Turini
          wrote on last edited by
          #4

          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

          H 1 Reply Last reply
          0
          • D Daniel Turini

            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

            H Offline
            H Offline
            Hugo Hallman
            wrote on last edited by
            #5

            Be warned! Using ::BeginThread with mfc corupts the mfc state handling! As far as I understand _beginthread is a wrapper for the Win32 API.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups