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 run a thread as a member function of a class?

How to run a thread as a member function of a class?

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestion
15 Posts 6 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.
  • E Eytukan

    i used CWinThread.. the error message i'm getting is TrayDialog.obj : error LNK2001: unresolved external symbol "public: __thiscall CTrayDialog::CTrayDialog(void)" (??0CTrayDialog@@QAE@XZ).Please help.. CWinThread *pThread; UINT Thread(LPVOID lp) { while(1) { AfxMessageBox("In Thread!"); Sleep(300); } return 0; } void CTrayDialog::TestThread() { CTrayDialog *obj; obj=new CTrayDialog; pThread=AfxBeginThread(Thread,obj); } thanks, vivek

    C Offline
    C Offline
    Cedric Moonen
    wrote on last edited by
    #5

    Can you post the header file of your class ? Please use formating tags (the code tag just above the emoticons).

    E 1 Reply Last reply
    0
    • E Eytukan

      forget everything.... i'll come with a simpler example.. please wait.:confused: vivek

      E Offline
      E Offline
      Eytukan
      wrote on last edited by
      #6

      Though it looks primitive.. please dont ignore.. see the class test with its data member "text" and function "disp()" i wanna access it from the Thread1 and Thread2.. #include #include #include char fValue[10]; CWinThread *pThread1,*pThread2; UINT Thread1(LPVOID lp) { CClientDC cdc(AfxGetApp()->m_pMainWnd); int j=250; for(int i=1;i<=600;i++) { cdc.TextOut(i,j,"Thread1"); cdc.SetBkColor(0x0009999); Sleep(6); strcpy(fValue,"Thread1"); Text = fValue; ///// I WANT TO ACCESS THE DATA MEMBER "TEXT" FROM CLASS TEST// cdc.TextOut(500,500,Text); } disp();/// HOW TO ACCESS MEMBER FUNCTION "DISP" FROM CLASS TEST// return 0; } UINT Thread2(LPVOID lp) { CClientDC cdc(AfxGetApp()->m_pMainWnd); cdc.SetBkColor(0x0990009); int j=280; for(int i=1;i<=600;i++) { cdc.TextOut(i,j,"Thread1"); Sleep(5); strcpy(fValue,"Thread2"); Text = fValue;///// I WANT TO ACCESS THE DATA MEMBER "TEXT" FROM CLASS "TEST"// cdc.TextOut(500,500,Text); } disp();/// HOW TO ACCESS MEMBER FUNCTION "DISP" FROM CLASS TEST// return 0; } class test { public: CString Text; void disp() { AfxMessageBox(Text); } }; CMenu cm; class MyWindow: public CFrameWnd , public test { public: MyWindow() { Create(0,"Threads of MFC"); } void q() { PostQuitMessage(0); } void Threads() { AfxMessageBox("Starting...."); } DECLARE_MESSAGE_MAP() }; BEGIN_MESSAGE_MAP(MyWindow,CFrameWnd) END_MESSAGE_MAP() class MyWin:public CWinApp { public : BOOL InitInstance() { MyWindow *x; x=new MyWindow; m_pMainWnd=x; pThread1=AfxBeginThread(Thread1,x); pThread2=AfxBeginThread(Thread2,x); x->ShowWindow(SW_SHOWMAXIMIZED); return 1; } }; MyWin App; thanks bob, vivek

      D B 2 Replies Last reply
      0
      • C Cedric Moonen

        Can you post the header file of your class ? Please use formating tags (the code tag just above the emoticons).

        E Offline
        E Offline
        Eytukan
        wrote on last edited by
        #7

        cedric, my program is too complex.. even i'm worn out looking at the code.. can u post a simple program which wil explain it clearly? should i declare the thread inside the class? just like we do for member functions? i.e class test { public UINT Thread(LPVOID lp); } ?? do we have any alternative for CWinThread? vivek

        E 1 Reply Last reply
        0
        • E Eytukan

          Though it looks primitive.. please dont ignore.. see the class test with its data member "text" and function "disp()" i wanna access it from the Thread1 and Thread2.. #include #include #include char fValue[10]; CWinThread *pThread1,*pThread2; UINT Thread1(LPVOID lp) { CClientDC cdc(AfxGetApp()->m_pMainWnd); int j=250; for(int i=1;i<=600;i++) { cdc.TextOut(i,j,"Thread1"); cdc.SetBkColor(0x0009999); Sleep(6); strcpy(fValue,"Thread1"); Text = fValue; ///// I WANT TO ACCESS THE DATA MEMBER "TEXT" FROM CLASS TEST// cdc.TextOut(500,500,Text); } disp();/// HOW TO ACCESS MEMBER FUNCTION "DISP" FROM CLASS TEST// return 0; } UINT Thread2(LPVOID lp) { CClientDC cdc(AfxGetApp()->m_pMainWnd); cdc.SetBkColor(0x0990009); int j=280; for(int i=1;i<=600;i++) { cdc.TextOut(i,j,"Thread1"); Sleep(5); strcpy(fValue,"Thread2"); Text = fValue;///// I WANT TO ACCESS THE DATA MEMBER "TEXT" FROM CLASS "TEST"// cdc.TextOut(500,500,Text); } disp();/// HOW TO ACCESS MEMBER FUNCTION "DISP" FROM CLASS TEST// return 0; } class test { public: CString Text; void disp() { AfxMessageBox(Text); } }; CMenu cm; class MyWindow: public CFrameWnd , public test { public: MyWindow() { Create(0,"Threads of MFC"); } void q() { PostQuitMessage(0); } void Threads() { AfxMessageBox("Starting...."); } DECLARE_MESSAGE_MAP() }; BEGIN_MESSAGE_MAP(MyWindow,CFrameWnd) END_MESSAGE_MAP() class MyWin:public CWinApp { public : BOOL InitInstance() { MyWindow *x; x=new MyWindow; m_pMainWnd=x; pThread1=AfxBeginThread(Thread1,x); pThread2=AfxBeginThread(Thread2,x); x->ShowWindow(SW_SHOWMAXIMIZED); return 1; } }; MyWin App; thanks bob, vivek

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

          viveking wrote: disp();/// HOW TO ACCESS MEMBER FUNCTION "DISP" FROM CLASS TEST// Try:

          UINT Thread2(LPVOID lp)
          {
          MyWindow *mw = (MyWindow *) lp;
          mw->disp();
          return 0;
          }


          "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

          1 Reply Last reply
          0
          • E Eytukan

            cedric, my program is too complex.. even i'm worn out looking at the code.. can u post a simple program which wil explain it clearly? should i declare the thread inside the class? just like we do for member functions? i.e class test { public UINT Thread(LPVOID lp); } ?? do we have any alternative for CWinThread? vivek

            E Offline
            E Offline
            Eytukan
            wrote on last edited by
            #9

            :(

            E 1 Reply Last reply
            0
            • E Eytukan

              i used CWinThread.. the error message i'm getting is TrayDialog.obj : error LNK2001: unresolved external symbol "public: __thiscall CTrayDialog::CTrayDialog(void)" (??0CTrayDialog@@QAE@XZ).Please help.. CWinThread *pThread; UINT Thread(LPVOID lp) { while(1) { AfxMessageBox("In Thread!"); Sleep(300); } return 0; } void CTrayDialog::TestThread() { CTrayDialog *obj; obj=new CTrayDialog; pThread=AfxBeginThread(Thread,obj); } thanks, vivek

              B Offline
              B Offline
              Bob Stanneveld
              wrote on last edited by
              #10

              Hello, Add the following lines of code to TrayDialog.cpp:

              CTrayDialog::CTrayDialog() : CDialog(NULL, CTrayDialog::IDD)
              {
              }

              What you are doing here is add the unresolved external symbol (the default contructor), but I think that you need an other solution: change the following code:

              void CTrayDialog::TestThread()
              {
              CTrayDialog *obj;
              obj=new CTrayDialog;
              pThread=AfxBeginThread(Thread,obj);
              }

              to:

              void CTrayDialog::TestThread()
              {
              CTrayDialog *obj;
              obj=new CTrayDialog(this);
              pThread=AfxBeginThread(Thread,obj);
              }

              Hope this helps. :-D Behind every great black man...             ... is the police. - Conspiracy brother Blog[^]

              E 1 Reply Last reply
              0
              • E Eytukan

                i used CWinThread.. the error message i'm getting is TrayDialog.obj : error LNK2001: unresolved external symbol "public: __thiscall CTrayDialog::CTrayDialog(void)" (??0CTrayDialog@@QAE@XZ).Please help.. CWinThread *pThread; UINT Thread(LPVOID lp) { while(1) { AfxMessageBox("In Thread!"); Sleep(300); } return 0; } void CTrayDialog::TestThread() { CTrayDialog *obj; obj=new CTrayDialog; pThread=AfxBeginThread(Thread,obj); } thanks, vivek

                M Offline
                M Offline
                Michael Dunn
                wrote on last edited by
                #11

                See the FAQ 6.1 Why can't I use a member function as a callback?[^] --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | 1ClickPicGrabber | CP SearchBar v2.0.2 | C++ Forum FAQ Strange things are afoot at the U+004B U+20DD

                1 Reply Last reply
                0
                • E Eytukan

                  i used CWinThread.. the error message i'm getting is TrayDialog.obj : error LNK2001: unresolved external symbol "public: __thiscall CTrayDialog::CTrayDialog(void)" (??0CTrayDialog@@QAE@XZ).Please help.. CWinThread *pThread; UINT Thread(LPVOID lp) { while(1) { AfxMessageBox("In Thread!"); Sleep(300); } return 0; } void CTrayDialog::TestThread() { CTrayDialog *obj; obj=new CTrayDialog; pThread=AfxBeginThread(Thread,obj); } thanks, vivek

                  P Offline
                  P Offline
                  Paul Steane
                  wrote on last edited by
                  #12

                  I suggest you have a look at Joseph M. Newcomer's articles on "Threads and Processes" at http://www.flounder.com/mvp_tips.htm[^]

                  E 1 Reply Last reply
                  0
                  • E Eytukan

                    Though it looks primitive.. please dont ignore.. see the class test with its data member "text" and function "disp()" i wanna access it from the Thread1 and Thread2.. #include #include #include char fValue[10]; CWinThread *pThread1,*pThread2; UINT Thread1(LPVOID lp) { CClientDC cdc(AfxGetApp()->m_pMainWnd); int j=250; for(int i=1;i<=600;i++) { cdc.TextOut(i,j,"Thread1"); cdc.SetBkColor(0x0009999); Sleep(6); strcpy(fValue,"Thread1"); Text = fValue; ///// I WANT TO ACCESS THE DATA MEMBER "TEXT" FROM CLASS TEST// cdc.TextOut(500,500,Text); } disp();/// HOW TO ACCESS MEMBER FUNCTION "DISP" FROM CLASS TEST// return 0; } UINT Thread2(LPVOID lp) { CClientDC cdc(AfxGetApp()->m_pMainWnd); cdc.SetBkColor(0x0990009); int j=280; for(int i=1;i<=600;i++) { cdc.TextOut(i,j,"Thread1"); Sleep(5); strcpy(fValue,"Thread2"); Text = fValue;///// I WANT TO ACCESS THE DATA MEMBER "TEXT" FROM CLASS "TEST"// cdc.TextOut(500,500,Text); } disp();/// HOW TO ACCESS MEMBER FUNCTION "DISP" FROM CLASS TEST// return 0; } class test { public: CString Text; void disp() { AfxMessageBox(Text); } }; CMenu cm; class MyWindow: public CFrameWnd , public test { public: MyWindow() { Create(0,"Threads of MFC"); } void q() { PostQuitMessage(0); } void Threads() { AfxMessageBox("Starting...."); } DECLARE_MESSAGE_MAP() }; BEGIN_MESSAGE_MAP(MyWindow,CFrameWnd) END_MESSAGE_MAP() class MyWin:public CWinApp { public : BOOL InitInstance() { MyWindow *x; x=new MyWindow; m_pMainWnd=x; pThread1=AfxBeginThread(Thread1,x); pThread2=AfxBeginThread(Thread2,x); x->ShowWindow(SW_SHOWMAXIMIZED); return 1; } }; MyWin App; thanks bob, vivek

                    B Offline
                    B Offline
                    Bob Stanneveld
                    wrote on last edited by
                    #13

                    Hello, Sorry for this late reply.. A couple of things:* You pass a pointer x, that points to a MyWindow object to the thread as parameter, but you don't use it in either of your threads..

                    • You call AfxGetApp() to get the value of m_pMainWnd

                    • Your main app window is x

                    • MyWindow derives from Test Thinking about all these things don't ring a bell? Anyway, what you want to achieve can be done in at least the following 2 ways:

                      static_cast<MyWindow*>(AfxGetApp()->m_pMainWnd)->Text;

                      static_cast<MyWindow*>(lp)->Text;

                      Hope this helps :-D Behind every great black man...             ... is the police. - Conspiracy brother Blog[^]

                    1 Reply Last reply
                    0
                    • E Eytukan

                      :(

                      E Offline
                      E Offline
                      Eytukan
                      wrote on last edited by
                      #14

                      bob,david,cedric,michael, and paul........ paul the link u gave is ultimate.. thank u so much. really resourceful.i think i wont be disturbing u guys again.. everything's there :) The Thread Solution It is almost always the case that you can use threads to do the job more easily. This is not without certain costs and hazards, but it ultimately is the better method. Here's a solution to handling the invert-everything. Note that we have to move from the pure-C domain (which we interface to via a static method) to the MFC domain. To the class (in this example, a CView-derived class), add the following declarations: static UINT run(LPVOID p); void run(); volatile BOOL running; To start a thread, your handler does void CMyView::doInvert() { running = TRUE; AfxBeginThread(run, this); } UINT CMyView::run(LPVOID p) { CMyView * me = (CMyView *)p; me->run(); return 0; } void CMyView::run() { for(int x=y = 0; running && y < image.height; y++) for(int x = 0; running && x < image.width; x++) changePixel(x, y); running = FALSE; } The command to stop the thread is very simple: void CMyView::OnStop() { running = FALSE; } That's all there is to it! :laugh: vivek

                      1 Reply Last reply
                      0
                      • P Paul Steane

                        I suggest you have a look at Joseph M. Newcomer's articles on "Threads and Processes" at http://www.flounder.com/mvp_tips.htm[^]

                        E Offline
                        E Offline
                        Eytukan
                        wrote on last edited by
                        #15

                        http://www.flounder.com/workerthreads.htm[^] thank u so much guys, vivek

                        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