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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Multithread

Multithread

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestion
16 Posts 9 Posters 1 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.
  • L Lost User

    change AfxBeginThread(DoTimer, 0); to AfxBeginThread(DoTimer, NULL);

    B Offline
    B Offline
    benjnp
    wrote on last edited by
    #3

    Josh Gray wrote: change AfxBeginThread(DoTimer, 0); to AfxBeginThread(DoTimer, NULL); didn't worked

    L 1 Reply Last reply
    0
    • B benjnp

      Josh Gray wrote: change AfxBeginThread(DoTimer, 0); to AfxBeginThread(DoTimer, NULL); didn't worked

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #4

      sorry my nmistake. Try AfxBeginThread( (AFX_THREADPROC)DoTimer, NULL);

      1 Reply Last reply
      0
      • B benjnp

        code: AfxBeginThread(DoTimer, 0); error: none of the 2 overloads can convert parameter 1 from type 'unsigned int (void *)' DoTimer declaration: UINT DoTimer(LPVOID pParam) { .... } what's wrong? I've seen it from an example.

        B Offline
        B Offline
        benjnp
        wrote on last edited by
        #5

        benjnp wrote: AfxBeginThread(DoTimer, 0); works fine as long as I don't call the functions that are built on the same class. Meaning, it can't call other functions or use the variables of that class. I want to create a thread for a specific function only, of course having the UINT type and (LPVOID param) parameter that may call/use other members of the class. How?

        C 1 Reply Last reply
        0
        • B benjnp

          code: AfxBeginThread(DoTimer, 0); error: none of the 2 overloads can convert parameter 1 from type 'unsigned int (void *)' DoTimer declaration: UINT DoTimer(LPVOID pParam) { .... } what's wrong? I've seen it from an example.

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

          Hi, Try casting the DoTimer to AFX_THREADPROC. regards, Rich "Programming today is a race between software engineers striving to build bigger and better idiot-proff programs, and the Universe trying to produce bigger and better idiots. So far the Universe is winning." -- Rich Cook

          B 1 Reply Last reply
          0
          • R RichardS

            Hi, Try casting the DoTimer to AFX_THREADPROC. regards, Rich "Programming today is a race between software engineers striving to build bigger and better idiot-proff programs, and the Universe trying to produce bigger and better idiots. So far the Universe is winning." -- Rich Cook

            B Offline
            B Offline
            benjnp
            wrote on last edited by
            #7

            RichardS wrote: Try casting the DoTimer to AFX_THREADPROC. How? a very simple snippet will do. Thanx :)

            R 1 Reply Last reply
            0
            • B benjnp

              code: AfxBeginThread(DoTimer, 0); error: none of the 2 overloads can convert parameter 1 from type 'unsigned int (void *)' DoTimer declaration: UINT DoTimer(LPVOID pParam) { .... } what's wrong? I've seen it from an example.

              T Offline
              T Offline
              Tim Smith
              wrote on last edited by
              #8

              First off, do NOT take the advice of people who say "Just cast it to "AFX_THREADPROC". The compiler is whining because something is wrong. Casting it just hides that without really fixing the advice. Very very very bad advice. Have you defined DoTimer inside of a class? If so, then don't. It should be a global function or a static member function in a class. Tim Smith I'm going to patent thought. I have yet to see any prior art.

              1 Reply Last reply
              0
              • B benjnp

                code: AfxBeginThread(DoTimer, 0); error: none of the 2 overloads can convert parameter 1 from type 'unsigned int (void *)' DoTimer declaration: UINT DoTimer(LPVOID pParam) { .... } what's wrong? I've seen it from an example.

                J Offline
                J Offline
                Jose Lamas Rios
                wrote on last edited by
                #9

                benjnp wrote: error: none of the 2 overloads can convert parameter 1 from type 'unsigned int (void *)' Cannot convert from type 'unsigned int (void *)' to what? That part of the error message is the key... AfxBeginThread expects the function you pass to be defined as follows:

                UINT __cdecl DoTimer(LPVOID pParam)
                {
                ....
                }

                -- jlr http://jlamas.blogspot.com/[^]

                B 1 Reply Last reply
                0
                • B benjnp

                  benjnp wrote: AfxBeginThread(DoTimer, 0); works fine as long as I don't call the functions that are built on the same class. Meaning, it can't call other functions or use the variables of that class. I want to create a thread for a specific function only, of course having the UINT type and (LPVOID param) parameter that may call/use other members of the class. How?

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

                  benjnp wrote: works fine as long as I don't call the functions that are built on the same class That's your problem ! You cannot use member functions of a class for that ? Why ? Simply because they have another calling convention than the "standard" functions: the this parameter is passed implicitely as parameter of the function. A way to have it working: use a global function as callback (instead of DoTimer) and pass the pointer of the current class as parameter (instead of the 0). So like that in your function you will be able to do something like: UINT MyGlobalFunc(LPVOID pParam) { CMyClass* pObject = (CMyClass*)pParam; pObject->DoTimer(); // Of course DoTimer must be public } And to start your thread: AfxBeginThread(MyGlobalFunc,this); Hope this helps

                  1 Reply Last reply
                  0
                  • B benjnp

                    code: AfxBeginThread(DoTimer, 0); error: none of the 2 overloads can convert parameter 1 from type 'unsigned int (void *)' DoTimer declaration: UINT DoTimer(LPVOID pParam) { .... } what's wrong? I've seen it from an example.

                    R Offline
                    R Offline
                    Roger Stoltz
                    wrote on last edited by
                    #11

                    If the thread function is a member function it has to be declared 'static'. Usually when I use worker threads I have one function for starting the thread and one for stopping it, all declared in the class. Before your app exits you want to be sure that you have no worker threads still running. Below is a skeleton to build your thread functionality on. You can read more about it in MSDN.

                    class CTest
                    {
                    CTest() { m_pThread = NULL; }
                    ~CTest() { StopThread(); }
                    .....
                    protected:
                    CWinThread* m_pThread;
                    volatile BOOL m_fStopThread;
                    BOOL StartThread()
                    {
                    StopThread();
                    m_fStopThread = FALSE;
                    m_pThread = AfxBeginThread( ThreadFn, this, 0, 0, CREATE_SUSPENDED, NULL );
                    if( m_pThread )
                    {
                    m_pThread->m_bAutoDelete = FALSE; // To be able to wait until it exits
                    m_pThread->ResumeThread();
                    }
                    return (m_pThread != NULL);
                    }
                    void StopThread()
                    {
                    m_fStopThread = TRUE;
                    if( m_pThread )
                    {
                    ::WaitForSingleObject( m_pThread->m_hThread, INFINITE );
                    delete m_pThread;
                    m_pThread = NULL;
                    }
                    }
                    static UINT ThreadFn( LPVOID pThis )
                    {
                    // This is the function passed as first parameter to AfxBeginThread
                    return ((Ctest*)pThis)->ThreadFn();
                    }
                    UINT ThreadFn()
                    {
                    // This is your thread function, return from it and the thread terminates
                    // You still have to clean up by deleting m_pThread though
                    while( !m_fStopThread );
                    return 0;
                    }
                    };

                    Hope this helps -- Roger

                    1 Reply Last reply
                    0
                    • B benjnp

                      code: AfxBeginThread(DoTimer, 0); error: none of the 2 overloads can convert parameter 1 from type 'unsigned int (void *)' DoTimer declaration: UINT DoTimer(LPVOID pParam) { .... } what's wrong? I've seen it from an example.

                      R Offline
                      R Offline
                      Rainer Schuster
                      wrote on last edited by
                      #12

                      You don't have to cast anything!! class CMyThread { public: CMyThread() {}; ~CMyThread() {}; static UINT MyStaticClassBeginThreadProc( LPVOID pParam); protected: int m_iSomeMember; int m_iThreadState; }; UINT CMyThread::MyStaticClassBeginThreadProc( LPVOID pParam) { //Do something usefull threadstuff!! //if you want to use classmembersm //pass a this pointer to the pParam //and cast it. i.e. the living object //for example: CMyThread *pSelf = (CMyThread *)pParam; pSelf->m_iSomeMember = 1; pSelf->m_iThreadState = 0; return 0; }; //.... or UINT MyGlobalBeginThreadProc( LPVOID pParam) { //Do something usefull threadstuff!! return 0; }; typedef struct { int iLen; int iState; char cType; } SOMETHREADDATA; //.... later somewhere in your code SOMETHREADDATA threadData; CWinThread *pThread; CMyThread thread; //initialize the class pThread = AfxBeginThread( MyGlobalBeginThreadProc, (LPVOID*)&threadData); pThread = AfxBeginThread( CMyThread::MyStaticClassBeginThreadProc, (LPVOID*)&thread); :->

                      B 1 Reply Last reply
                      0
                      • B benjnp

                        code: AfxBeginThread(DoTimer, 0); error: none of the 2 overloads can convert parameter 1 from type 'unsigned int (void *)' DoTimer declaration: UINT DoTimer(LPVOID pParam) { .... } what's wrong? I've seen it from an example.

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

                        can u post the declaration of your threads? (that u may have declared in the header file) V

                        1 Reply Last reply
                        0
                        • B benjnp

                          RichardS wrote: Try casting the DoTimer to AFX_THREADPROC. How? a very simple snippet will do. Thanx :)

                          R Offline
                          R Offline
                          RichardS
                          wrote on last edited by
                          #14

                          AfxBeginThread ((AFX_THREADPROC)DoTimer, 0); BTW, the reason for the cast is that there are calling symantics between C and C++. The function to create threads needs C calling scheme but the function is a C++ function (even though it is not part of class). By casting it you are not "breaking" anything you are mearly telling the compiler to treat the function as a C function, which it can be. regards, Rich "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far the Universe is winning." -- Rich Cook

                          1 Reply Last reply
                          0
                          • J Jose Lamas Rios

                            benjnp wrote: error: none of the 2 overloads can convert parameter 1 from type 'unsigned int (void *)' Cannot convert from type 'unsigned int (void *)' to what? That part of the error message is the key... AfxBeginThread expects the function you pass to be defined as follows:

                            UINT __cdecl DoTimer(LPVOID pParam)
                            {
                            ....
                            }

                            -- jlr http://jlamas.blogspot.com/[^]

                            B Offline
                            B Offline
                            benjnp
                            wrote on last edited by
                            #15

                            that's the whole error msg, I'm also looking for it when i first saw it

                            1 Reply Last reply
                            0
                            • R Rainer Schuster

                              You don't have to cast anything!! class CMyThread { public: CMyThread() {}; ~CMyThread() {}; static UINT MyStaticClassBeginThreadProc( LPVOID pParam); protected: int m_iSomeMember; int m_iThreadState; }; UINT CMyThread::MyStaticClassBeginThreadProc( LPVOID pParam) { //Do something usefull threadstuff!! //if you want to use classmembersm //pass a this pointer to the pParam //and cast it. i.e. the living object //for example: CMyThread *pSelf = (CMyThread *)pParam; pSelf->m_iSomeMember = 1; pSelf->m_iThreadState = 0; return 0; }; //.... or UINT MyGlobalBeginThreadProc( LPVOID pParam) { //Do something usefull threadstuff!! return 0; }; typedef struct { int iLen; int iState; char cType; } SOMETHREADDATA; //.... later somewhere in your code SOMETHREADDATA threadData; CWinThread *pThread; CMyThread thread; //initialize the class pThread = AfxBeginThread( MyGlobalBeginThreadProc, (LPVOID*)&threadData); pThread = AfxBeginThread( CMyThread::MyStaticClassBeginThreadProc, (LPVOID*)&thread); :->

                              B Offline
                              B Offline
                              benjnp
                              wrote on last edited by
                              #16

                              I tried your idea and it work a bit when accessing a member function of the same class, BUT that member function must be stand-alone. I mean. if I put or access some member variables of the same class, a run-time error occured (MFC4d.dll has an error).

                              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