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() with a function in the main class

AfxBeginThread() with a function in the main class

Scheduled Pinned Locked Moved C / C++ / MFC
c++comhelpquestion
23 Posts 5 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.
  • J Johpoke

    This is allways something wich i have had trouble with, usualy i end up doing some ugly trick to make it work.. But this time i cant.. (well ive been spending around 3 days trying..) I need a thread which is connected to the main class of my MFC Dialog based application. I looked at Code Project:Using AfxBeginThread...[^] In which i end up createing a new static function that seems to work with some functions.. But when i tried calling the function i wanted it to run, it couldnt since it wasnt a static function.. So i tried making it a static function, but then i got loads of error messages... So how would i go about creating and running a thread that is connected to the main class (like "CThreadDemoDlg::OnStart()" ) ? The function doesnt need any params or other advanced features, maybe i should start it using a differnt function? Or how should i go about this? thanks! PS: Im not really that good at the terminology for c++, or c++ programming itself, so nothing too hightech or advanced unless its a must do..

    //Johannes

    L Offline
    L Offline
    led mike
    wrote on last edited by
    #2

    Well this is not a MFC thread but it can work in MFC applications and/or maybe it will help give you the idea

    /** Win32 CreateThread class wrapper */
    class base_w32thread{
    protected:
        HANDLE    _handle;
        DWORD    _dwTID;
        base_w32thread():_handle(0), _dwTID(0L){}
        virtual ~base_w32thread()=0;

    public:
        bool start(LPSECURITY_ATTRIBUTES psecattrs = NULL, DWORD dwCreateFlags = 0L);
        operator HANDLE(){ return _handle; }
    protected:
        virtual void run()=0;
        virtual void onEndThread(){}
        static long WINAPI threadfnc( LPARAM lp);
    };

    #include "StdAfx.h"

    #include "base_w32thread.h"
    #include <winbase.h>
    #include <assert.h>

    base_w32thread::~base_w32thread(){}

    /** start the thread */
    bool base_w32thread::start(LPSECURITY_ATTRIBUTES psecattrs, DWORD dwCreateFlags){

    assert( !_handle);
        if( _handle)
            return false;

    _handle = ::CreateThread( psecattrs, 0,
            (LPTHREAD_START_ROUTINE)threadfnc, this, dwCreateFlags, &_dwTID);
        return (_handle)?true:false;
    }
    /** Thread function */
    long WINAPI base_w32thread::threadfnc( LPARAM lp){

    assert( lp);
        base_w32thread* pThis = (base_w32thread*)lp;
        if( pThis)
            pThis->run();

    pThis->_handle = 0L;        // thread exited
        pThis->onEndThread();
        return 0L;
    }

    1 Reply Last reply
    0
    • J Johpoke

      This is allways something wich i have had trouble with, usualy i end up doing some ugly trick to make it work.. But this time i cant.. (well ive been spending around 3 days trying..) I need a thread which is connected to the main class of my MFC Dialog based application. I looked at Code Project:Using AfxBeginThread...[^] In which i end up createing a new static function that seems to work with some functions.. But when i tried calling the function i wanted it to run, it couldnt since it wasnt a static function.. So i tried making it a static function, but then i got loads of error messages... So how would i go about creating and running a thread that is connected to the main class (like "CThreadDemoDlg::OnStart()" ) ? The function doesnt need any params or other advanced features, maybe i should start it using a differnt function? Or how should i go about this? thanks! PS: Im not really that good at the terminology for c++, or c++ programming itself, so nothing too hightech or advanced unless its a must do..

      //Johannes

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

      What's wrong with:

      void CMyDlg::Process( void )
      {
      // do whatever you need to in this non-static method
      }
      ...
      /* static */ UINT CMyDlg::Process( LPVOID pParam )
      {
      CMyDlg *pDlg = (CMyDlg *) pParam;

      pDlg->Process();
      
      return 0;
      

      }
      ...
      AfxBeginThread(Process, this);


      "A good athlete is the result of a good and worthy opponent." - David Crow

      "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

      1 Reply Last reply
      0
      • J Johpoke

        This is allways something wich i have had trouble with, usualy i end up doing some ugly trick to make it work.. But this time i cant.. (well ive been spending around 3 days trying..) I need a thread which is connected to the main class of my MFC Dialog based application. I looked at Code Project:Using AfxBeginThread...[^] In which i end up createing a new static function that seems to work with some functions.. But when i tried calling the function i wanted it to run, it couldnt since it wasnt a static function.. So i tried making it a static function, but then i got loads of error messages... So how would i go about creating and running a thread that is connected to the main class (like "CThreadDemoDlg::OnStart()" ) ? The function doesnt need any params or other advanced features, maybe i should start it using a differnt function? Or how should i go about this? thanks! PS: Im not really that good at the terminology for c++, or c++ programming itself, so nothing too hightech or advanced unless its a must do..

        //Johannes

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

        The thread procedure accepts a void pointer which you can pass when you create the thread. If you pass a pointer to an object of a class then you will have access to that object from the thread.  If the thread proc is a static member function of that class, you have complete access (to public/protected/private members).

        class CThreadDemoDlg : public CDialog
        {
        static UINT __cdecl MyThreadProc(LPVOID pParam); 
        protected:
           CWinThread *pAnotherThread;
        public:
           void SomeThreadStarterFunc();
        };

        void CThreadDemoDlg::SomeThreadStarterFunc()
        {
        pAnotherThread = AfxBeginThread(&CThreadDemoDlg::MyThreadProc, this);
        }

        UINT __cdecl CThreadDemoDlg::MyThreadProc(LPVOID pParam)
        {
        CThreadDemoDlg *pMyDlg = (CThreadDemoDlg *)pParam;

        ...
        }

        Mark


        Last modified: 25mins after originally posted --

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

        J 1 Reply Last reply
        0
        • M Mark Salsbery

          The thread procedure accepts a void pointer which you can pass when you create the thread. If you pass a pointer to an object of a class then you will have access to that object from the thread.  If the thread proc is a static member function of that class, you have complete access (to public/protected/private members).

          class CThreadDemoDlg : public CDialog
          {
          static UINT __cdecl MyThreadProc(LPVOID pParam); 
          protected:
             CWinThread *pAnotherThread;
          public:
             void SomeThreadStarterFunc();
          };

          void CThreadDemoDlg::SomeThreadStarterFunc()
          {
          pAnotherThread = AfxBeginThread(&CThreadDemoDlg::MyThreadProc, this);
          }

          UINT __cdecl CThreadDemoDlg::MyThreadProc(LPVOID pParam)
          {
          CThreadDemoDlg *pMyDlg = (CThreadDemoDlg *)pParam;

          ...
          }

          Mark


          Last modified: 25mins after originally posted --

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

          J Offline
          J Offline
          Johpoke
          wrote on last edited by
          #5

          Thank you all for you great replies :) Im trying out Mark's code right now, but its giving me an error message i really dont understand.. On this line: CThreadDemoDlg *pMyDlg = (CThreadDemoDlg *pParam); error C2146: syntax error : missing ')' before identifier 'pParam' error C2059: syntax error : ')' Ive renamed all the classes to the class on my program, so its not that... What could this be? Thank you all! :)

          //Johannes

          M L 2 Replies Last reply
          0
          • J Johpoke

            Thank you all for you great replies :) Im trying out Mark's code right now, but its giving me an error message i really dont understand.. On this line: CThreadDemoDlg *pMyDlg = (CThreadDemoDlg *pParam); error C2146: syntax error : missing ')' before identifier 'pParam' error C2059: syntax error : ')' Ive renamed all the classes to the class on my program, so its not that... What could this be? Thank you all! :)

            //Johannes

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

            Fixed.  Sorry about that :) It should be

            CThreadDemoDlg *pMyDlg = (CThreadDemoDlg *)pParam;

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

            J 1 Reply Last reply
            0
            • M Mark Salsbery

              Fixed.  Sorry about that :) It should be

              CThreadDemoDlg *pMyDlg = (CThreadDemoDlg *)pParam;

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

              J Offline
              J Offline
              Johpoke
              wrote on last edited by
              #7

              Ah yes now it compiles, thanks. But when i add a function to it (one that is connected to CThreadDemoDlg) i get the cant call a nonstatic member function.. error C2352: 'CThreadDemoDlg::SomeFunction' : illegal call of non-static member function see declaration of 'SomeFunction' So how should i go about calling the function? thanks again!

              //Johannes

              D A 2 Replies Last reply
              0
              • J Johpoke

                Ah yes now it compiles, thanks. But when i add a function to it (one that is connected to CThreadDemoDlg) i get the cant call a nonstatic member function.. error C2352: 'CThreadDemoDlg::SomeFunction' : illegal call of non-static member function see declaration of 'SomeFunction' So how should i go about calling the function? thanks again!

                //Johannes

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

                Johpoke wrote:

                So how should i go about calling the function?

                Can you at least bother to show the code that is producing this error? :rolleyes:


                "A good athlete is the result of a good and worthy opponent." - David Crow

                "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                J 1 Reply Last reply
                0
                • D David Crow

                  Johpoke wrote:

                  So how should i go about calling the function?

                  Can you at least bother to show the code that is producing this error? :rolleyes:


                  "A good athlete is the result of a good and worthy opponent." - David Crow

                  "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                  J Offline
                  J Offline
                  Johpoke
                  wrote on last edited by
                  #9

                  Doh, UINT __cdecl CThreadDemoDlg::MyThreadProc(LPVOID pParam) { CThreadDemoDlg *pMyDlg = (CThreadDemoDlg *)pParam; MyFunction(); return 1; } It looks exactly like that, except the names are different

                  //Johannes

                  D 1 Reply Last reply
                  0
                  • J Johpoke

                    Doh, UINT __cdecl CThreadDemoDlg::MyThreadProc(LPVOID pParam) { CThreadDemoDlg *pMyDlg = (CThreadDemoDlg *)pParam; MyFunction(); return 1; } It looks exactly like that, except the names are different

                    //Johannes

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

                    Hint: Ever wonder what pMyDlg is being used for?


                    "A good athlete is the result of a good and worthy opponent." - David Crow

                    "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                    J 1 Reply Last reply
                    0
                    • J Johpoke

                      Thank you all for you great replies :) Im trying out Mark's code right now, but its giving me an error message i really dont understand.. On this line: CThreadDemoDlg *pMyDlg = (CThreadDemoDlg *pParam); error C2146: syntax error : missing ')' before identifier 'pParam' error C2059: syntax error : ')' Ive renamed all the classes to the class on my program, so its not that... What could this be? Thank you all! :)

                      //Johannes

                      L Offline
                      L Offline
                      led mike
                      wrote on last edited by
                      #11

                      Johpoke wrote:

                      but its giving me an error message i really dont understand..

                      You don't understand a syntax error message but you are working with multi threading *sigh* different day same old garbage

                      M 1 Reply Last reply
                      0
                      • D David Crow

                        Hint: Ever wonder what pMyDlg is being used for?


                        "A good athlete is the result of a good and worthy opponent." - David Crow

                        "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                        J Offline
                        J Offline
                        Johpoke
                        wrote on last edited by
                        #12

                        I was thinking i must use it to gain access to the rest of the functions so i like tried :: on it but that didnt work. Now i tried -> on it and all my functions come up :D Thanks alot for the help. The only downside with this is im probably gonna have to go through loads of old programs and get rid of their ugly methods and add this. But i guess its worth it :-D Thanks again :)

                        //Johannes

                        1 Reply Last reply
                        0
                        • L led mike

                          Johpoke wrote:

                          but its giving me an error message i really dont understand..

                          You don't understand a syntax error message but you are working with multi threading *sigh* different day same old garbage

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

                          led mike wrote:

                          *sigh* different day same old garbage

                          This[^] should cheer you up. :beer:

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

                          L 1 Reply Last reply
                          0
                          • M Mark Salsbery

                            led mike wrote:

                            *sigh* different day same old garbage

                            This[^] should cheer you up. :beer:

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

                            L Offline
                            L Offline
                            led mike
                            wrote on last edited by
                            #14

                            Good Grief :sigh: Happy Friday :beer: :jig:

                            J M 2 Replies Last reply
                            0
                            • L led mike

                              Good Grief :sigh: Happy Friday :beer: :jig:

                              J Offline
                              J Offline
                              Johpoke
                              wrote on last edited by
                              #15

                              lol, that dancing dude is funny/weird. My program now works perfectly, thank you all for you help. How does one make that dude anyway? i cant find him...

                              //Johannes

                              L M 2 Replies Last reply
                              0
                              • J Johpoke

                                lol, that dancing dude is funny/weird. My program now works perfectly, thank you all for you help. How does one make that dude anyway? i cant find him...

                                //Johannes

                                L Offline
                                L Offline
                                led mike
                                wrote on last edited by
                                #16

                                Johpoke wrote:

                                My program now works perfectly

                                If it works, do it.

                                If it works, do it it works

                                seems to work      probably
                                     ^                  ^
                                If it   works, do it it    works

                                seems to work      probably
                                     ^                  ^
                                If it   works, do it it    works

                                (and might work again)

                                Cogito ergo sum

                                (although, admittedly, that is an assumption)

                                1 Reply Last reply
                                0
                                • L led mike

                                  Good Grief :sigh: Happy Friday :beer: :jig:

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

                                  :beer:

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

                                  1 Reply Last reply
                                  0
                                  • J Johpoke

                                    lol, that dancing dude is funny/weird. My program now works perfectly, thank you all for you help. How does one make that dude anyway? i cant find him...

                                    //Johannes

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

                                    Johpoke wrote:

                                    How does one make that dude anyway? i cant find him...

                                    It's right next to the :beer: one. With a little ingenuity, you'll find it :)

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

                                    J 1 Reply Last reply
                                    0
                                    • M Mark Salsbery

                                      Johpoke wrote:

                                      How does one make that dude anyway? i cant find him...

                                      It's right next to the :beer: one. With a little ingenuity, you'll find it :)

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

                                      J Offline
                                      J Offline
                                      Johpoke
                                      wrote on last edited by
                                      #19

                                      Maybe its like a gold-member only emotion. or maybe it doesnt like me... Ah well doesnt matter im happy anyway :)

                                      //Johannes

                                      M 1 Reply Last reply
                                      0
                                      • J Johpoke

                                        Maybe its like a gold-member only emotion. or maybe it doesnt like me... Ah well doesnt matter im happy anyway :)

                                        //Johannes

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

                                        Johpoke wrote:

                                        Maybe its like a gold-member only emotion.

                                        Nope.  I was so proud when I found it all by myself, since I know nothing about web programming :) ;P

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

                                        J 1 Reply Last reply
                                        0
                                        • M Mark Salsbery

                                          Johpoke wrote:

                                          Maybe its like a gold-member only emotion.

                                          Nope.  I was so proud when I found it all by myself, since I know nothing about web programming :) ;P

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

                                          J Offline
                                          J Offline
                                          Johpoke
                                          wrote on last edited by
                                          #21

                                          :jig: :~ :jig:

                                          //Johannes

                                          M 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