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.
  • 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
                                    • J Johpoke

                                      :jig: :~ :jig:

                                      //Johannes

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

                                      :laugh:

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

                                      1 Reply 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

                                        A Offline
                                        A Offline
                                        Ahmad Al Maraghi
                                        wrote on last edited by
                                        #23

                                        u don't understand the problem:(

                                        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