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

Problem in AfxBeginThread

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
4 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.
  • G Offline
    G Offline
    gReaen
    wrote on last edited by
    #1

    I need to call the function AfxBeginThread in one of my function (say, myFunc). myFunc should receive a function name which should become a thread. My code is as follows: void myFunc(void* fthread) { HANDLE cmd_thread; CWinThread* pCmdThread = AfxBeginThread(fthread, NULL); cmd_thread = pCmdThread->m_hThread; .......... } But i get an error that: cannot convert parameter one of AfxBeginThread from void* to unsigned int(_cdecl*)(void *)..... What should the parameter type be?

    C I 2 Replies Last reply
    0
    • G gReaen

      I need to call the function AfxBeginThread in one of my function (say, myFunc). myFunc should receive a function name which should become a thread. My code is as follows: void myFunc(void* fthread) { HANDLE cmd_thread; CWinThread* pCmdThread = AfxBeginThread(fthread, NULL); cmd_thread = pCmdThread->m_hThread; .......... } But i get an error that: cannot convert parameter one of AfxBeginThread from void* to unsigned int(_cdecl*)(void *)..... What should the parameter type be?

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

      gReaen wrote:

      myFunc should receive a function name which should become a thread

      In your example, you don't provide a function name but a void pointer. What do really need to do ? To provide the name of the function (so, a string containing the function name, like "MyThreadFunc") or a function pointer ? The first option is much more complicated because there is no way for the compiler to get a function pointer just with its name (its like trying to get a variable with a string containing its name). For the second option, that's much more easier: you just pass the function pointer that can be passed to the AfxBeginThread. So, can you be more clear on that point ?

      Cédric Moonen Software developer
      Charting control [v1.2]

      1 Reply Last reply
      0
      • G gReaen

        I need to call the function AfxBeginThread in one of my function (say, myFunc). myFunc should receive a function name which should become a thread. My code is as follows: void myFunc(void* fthread) { HANDLE cmd_thread; CWinThread* pCmdThread = AfxBeginThread(fthread, NULL); cmd_thread = pCmdThread->m_hThread; .......... } But i get an error that: cannot convert parameter one of AfxBeginThread from void* to unsigned int(_cdecl*)(void *)..... What should the parameter type be?

        I Offline
        I Offline
        Iain Clarke Warrior Programmer
        wrote on last edited by
        #3

        To expand on what Cedric has said... Why is MyFunc taking a void? Would it not be a good idea to make the parameter of the same pointer type as AfxBeginThread is wanting? That was you can avoid errors in other places in your code. This is from VC++6 - The prototype may have changed in later versions, but I doubt it. Just be sure to check.

        oid myFunc(AFX_THREADPROC pfnThreadProc, LPVOID pParam)
        {
        HANDLE cmd_thread;
        CWinThread* pCmdThread = AfxBeginThread(pfnThreadProc, pParam);
        cmd_thread = pCmdThread->m_hThread;
        ..........
        }

        It's rare for me to not pass a parameter to a thread - after all, it needs to know why it exists... Or at least a window handle to post an "I'm done" message. To answer your next question, functions you pass must have the following prototype to be valid for AfxBeginThread:

        typedef UINT (AFX_CDECL *AFX_THREADPROC)(LPVOID);

        eg:
        UINT AFX_CDECL MyThread (void *)
        {
        // exciting thread!
        Sleep (10000);
        return 97;
        }

        I hope that helped. Iain.

        G 1 Reply Last reply
        0
        • I Iain Clarke Warrior Programmer

          To expand on what Cedric has said... Why is MyFunc taking a void? Would it not be a good idea to make the parameter of the same pointer type as AfxBeginThread is wanting? That was you can avoid errors in other places in your code. This is from VC++6 - The prototype may have changed in later versions, but I doubt it. Just be sure to check.

          oid myFunc(AFX_THREADPROC pfnThreadProc, LPVOID pParam)
          {
          HANDLE cmd_thread;
          CWinThread* pCmdThread = AfxBeginThread(pfnThreadProc, pParam);
          cmd_thread = pCmdThread->m_hThread;
          ..........
          }

          It's rare for me to not pass a parameter to a thread - after all, it needs to know why it exists... Or at least a window handle to post an "I'm done" message. To answer your next question, functions you pass must have the following prototype to be valid for AfxBeginThread:

          typedef UINT (AFX_CDECL *AFX_THREADPROC)(LPVOID);

          eg:
          UINT AFX_CDECL MyThread (void *)
          {
          // exciting thread!
          Sleep (10000);
          return 97;
          }

          I hope that helped. Iain.

          G Offline
          G Offline
          gReaen
          wrote on last edited by
          #4

          I had to send a function pointer. Thanks for pointing that out.

          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