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

AfxBeginThread

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

    i have a function that i want a worker thread to execute it, my function is of the type bool wmi(IP) Can i use AfxBeginThread() with this or can only void functions be called? how can i pass a variable back or do i have to use a pointer? thanks

    I D 2 Replies Last reply
    0
    • V viperlogic

      i have a function that i want a worker thread to execute it, my function is of the type bool wmi(IP) Can i use AfxBeginThread() with this or can only void functions be called? how can i pass a variable back or do i have to use a pointer? thanks

      I Offline
      I Offline
      includeh10
      wrote on last edited by
      #2

      if u want to pass a variable back, u must use a pointer. code is in this way: ...ThreadFunction(void*pv) { SomeClass*pc=(SomeClass*)pv; pc->SomeVariableBack=...; } it may not be what u want ...


      A special image tool for Windows C++ programmers, don't miss it! The world unique Software Label Maker is waiting for you and me ... A nice hyper tool for optimizing your Microsoft html-help contents.

      1 Reply Last reply
      0
      • V viperlogic

        i have a function that i want a worker thread to execute it, my function is of the type bool wmi(IP) Can i use AfxBeginThread() with this or can only void functions be called? how can i pass a variable back or do i have to use a pointer? thanks

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        viperlogic wrote:

        Can i use AfxBeginThread() with this

        Indirectly you can. Something like:

        AfxBeginThread(ThreadProc);
        ...
        UINT ThreadProc( LPVOID lpParam )
        {
        wmi();
        }

        You'll likely need to use the second argument to AfxBeginThread() as a way to call or communicate with the wmi() function.


        "Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain

        "There is no death, only a change of worlds." - Native American Proverb

        V 1 Reply Last reply
        0
        • D David Crow

          viperlogic wrote:

          Can i use AfxBeginThread() with this

          Indirectly you can. Something like:

          AfxBeginThread(ThreadProc);
          ...
          UINT ThreadProc( LPVOID lpParam )
          {
          wmi();
          }

          You'll likely need to use the second argument to AfxBeginThread() as a way to call or communicate with the wmi() function.


          "Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain

          "There is no death, only a change of worlds." - Native American Proverb

          V Offline
          V Offline
          viperlogic
          wrote on last edited by
          #4

          thx for the reply how would i implement a timer, so that after say 5secs if the thread hasnt finished, kill it?

          D 1 Reply Last reply
          0
          • V viperlogic

            thx for the reply how would i implement a timer, so that after say 5secs if the thread hasnt finished, kill it?

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            Use WaitForSingleObject() for this.

            CWinThread *pThread = AfxBeginThread(..., CREATE_SUSPENDED);
            pThread->m_bAutoDelete = FALSE;
            pThread->ResumeThread();
            ...
            switch(WaitForSingleObject(pThread->m_hThread, 5000))
            {
            case WAIT_TIMEOUT:
            // need to kill the thread
            break;

            case WAIT\_OBJECT\_0:
                // thread has terminated on its own
                break;
            

            }


            "Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain

            "There is no death, only a change of worlds." - Native American Proverb

            V 1 Reply Last reply
            0
            • D David Crow

              Use WaitForSingleObject() for this.

              CWinThread *pThread = AfxBeginThread(..., CREATE_SUSPENDED);
              pThread->m_bAutoDelete = FALSE;
              pThread->ResumeThread();
              ...
              switch(WaitForSingleObject(pThread->m_hThread, 5000))
              {
              case WAIT_TIMEOUT:
              // need to kill the thread
              break;

              case WAIT\_OBJECT\_0:
                  // thread has terminated on its own
                  break;
              

              }


              "Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain

              "There is no death, only a change of worlds." - Native American Proverb

              V Offline
              V Offline
              viperlogic
              wrote on last edited by
              #6

              thx for the replys i am having trouble implementing AfxBeginThread() void CScanDlg::OnButtonScan() { AfxBeginThread(ThreadProc); } UINT CScanDlg::ThreadProc(LPVOID pParam) { if(wmi(strIP1)) return 0; else return 1; } what is wrong above, sorry about these questions but this is new to me

              D 1 Reply Last reply
              0
              • V viperlogic

                thx for the replys i am having trouble implementing AfxBeginThread() void CScanDlg::OnButtonScan() { AfxBeginThread(ThreadProc); } UINT CScanDlg::ThreadProc(LPVOID pParam) { if(wmi(strIP1)) return 0; else return 1; } what is wrong above, sorry about these questions but this is new to me

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #7

                viperlogic wrote:

                i am having trouble implementing AfxBeginThread()...

                Aside from not suppling enough arguments, what troubles are you having?

                viperlogic wrote:

                UINT CScanDlg::ThreadProc(LPVOID pParam)

                Is this a static member?


                "Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain

                "There is no death, only a change of worlds." - Native American Proverb

                V 1 Reply Last reply
                0
                • D David Crow

                  viperlogic wrote:

                  i am having trouble implementing AfxBeginThread()...

                  Aside from not suppling enough arguments, what troubles are you having?

                  viperlogic wrote:

                  UINT CScanDlg::ThreadProc(LPVOID pParam)

                  Is this a static member?


                  "Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain

                  "There is no death, only a change of worlds." - Native American Proverb

                  V Offline
                  V Offline
                  viperlogic
                  wrote on last edited by
                  #8

                  ok this is what i have.. static UINT CScanDlg::ThreadProc(LPVOID pParam); delcared in the header file AfxBeginThread(ThreadProc,NULL,THREAD_PRIORITY_NORMAL,0,0,NULL); UINT CScanDlg::ThreadProc(LPVOID pParam) { if(wmi(strIP1)) return 0; else return 1; } i get the following error CScanDlg::wmi' : illegal call of non-static member function

                  D 1 Reply Last reply
                  0
                  • V viperlogic

                    ok this is what i have.. static UINT CScanDlg::ThreadProc(LPVOID pParam); delcared in the header file AfxBeginThread(ThreadProc,NULL,THREAD_PRIORITY_NORMAL,0,0,NULL); UINT CScanDlg::ThreadProc(LPVOID pParam) { if(wmi(strIP1)) return 0; else return 1; } i get the following error CScanDlg::wmi' : illegal call of non-static member function

                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #9

                    Try:

                    AfxBeginThread(ThreadProc, this);
                    ...
                    UINT CScanDlg::ThreadProc( LPVOID pParam )
                    {
                    CScanDlg *pDlg = (CScanDlg *) pParam;

                    if (pDlg->wmi(strIP1))
                        return 0;
                    else
                        return 1;
                    

                    }


                    "Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain

                    "There is no death, only a change of worlds." - Native American Proverb

                    V 1 Reply Last reply
                    0
                    • D David Crow

                      Try:

                      AfxBeginThread(ThreadProc, this);
                      ...
                      UINT CScanDlg::ThreadProc( LPVOID pParam )
                      {
                      CScanDlg *pDlg = (CScanDlg *) pParam;

                      if (pDlg->wmi(strIP1))
                          return 0;
                      else
                          return 1;
                      

                      }


                      "Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain

                      "There is no death, only a change of worlds." - Native American Proverb

                      V Offline
                      V Offline
                      viperlogic
                      wrote on last edited by
                      #10

                      thx, that worked can explain why thou?

                      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