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. threads

threads

Scheduled Pinned Locked Moved C / C++ / MFC
question
6 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.
  • R Offline
    R Offline
    Russell
    wrote on last edited by
    #1

    Is there any Macro that can be used to know if the function that is running is called from a worker-thread or the main program? It is needed to know without use any addictional static global variable or addictional parameter to the function. thanks


    Russell

    R 1 Reply Last reply
    0
    • R Russell

      Is there any Macro that can be used to know if the function that is running is called from a worker-thread or the main program? It is needed to know without use any addictional static global variable or addictional parameter to the function. thanks


      Russell

      R Offline
      R Offline
      Russell
      wrote on last edited by
      #2

      This looks the solution:

      if( AfxGetMainWnd()==NULL ){
      // Thread specialization
      }else{
      // Main program specialization
      }

      Any comment will be wellcome


      Russell

      R 1 Reply Last reply
      0
      • R Russell

        This looks the solution:

        if( AfxGetMainWnd()==NULL ){
        // Thread specialization
        }else{
        // Main program specialization
        }

        Any comment will be wellcome


        Russell

        R Offline
        R Offline
        ramana g
        wrote on last edited by
        #3

        May be you can use something like this... DWORD dwMainThreadId = 0; DWORD dwProcessId = 0; dwMainThreadId = GetWindowThreadProcessId( hMainWnd, &dwProcessId); if( dwMainThreadId == GetCurrentThreadId() ) { //Main Thread } else { //Thread specialization code } I have not tested this snippet.

        R 1 Reply Last reply
        0
        • R ramana g

          May be you can use something like this... DWORD dwMainThreadId = 0; DWORD dwProcessId = 0; dwMainThreadId = GetWindowThreadProcessId( hMainWnd, &dwProcessId); if( dwMainThreadId == GetCurrentThreadId() ) { //Main Thread } else { //Thread specialization code } I have not tested this snippet.

          R Offline
          R Offline
          Russell
          wrote on last edited by
          #4

          Yes! your solution looks work better then mine

          inline BOOL IsThisAThread(){
          return GetCurrentThreadId()==GetWindowThreadProcessId(AfxGetMainWnd()->GetSafeHwnd(), NULL);
          }

          thanks


          Russell

          M 1 Reply Last reply
          0
          • R Russell

            Yes! your solution looks work better then mine

            inline BOOL IsThisAThread(){
            return GetCurrentThreadId()==GetWindowThreadProcessId(AfxGetMainWnd()->GetSafeHwnd(), NULL);
            }

            thanks


            Russell

            M Offline
            M Offline
            Mark Salsbery
            wrote on last edited by
            #5

            *Edit* Never mind - that error doesn't occur :) Mark

            Mark Salsbery Microsoft MVP - Visual C++ :java:

            modified on Tuesday, September 23, 2008 4:56 PM

            R 1 Reply Last reply
            0
            • M Mark Salsbery

              *Edit* Never mind - that error doesn't occur :) Mark

              Mark Salsbery Microsoft MVP - Visual C++ :java:

              modified on Tuesday, September 23, 2008 4:56 PM

              R Offline
              R Offline
              Russell
              wrote on last edited by
              #6

              yes, you are right....but as you suggested it is better in this way, thanks

              inline BOOL IsThisAThread(){
              CWnd* pMainWnd=AfxGetMainWnd();
              if(! pMainWnd) return TRUE;
              return GetCurrentThreadId()!=GetWindowThreadProcessId(pMainWnd->GetSafeHwnd(), NULL);
              }


              Russell

              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