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. Creating a thread

Creating a thread

Scheduled Pinned Locked Moved C / C++ / MFC
csharpvisual-studioregexhelp
8 Posts 3 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 Offline
    L Offline
    laiju
    wrote on last edited by
    #1

    Following is a funciton where I am calling a thread void CMyView::OnInitialUpdate() { ... AfxBeginThread(Thread_ScanConnectionState, m_sComPort, THREAD_PRIORITY_NORMAL, NULL, NULL, NULL); ... } //Foll is the thread definition UINT CMyView::Thread_ScanConnectionState(LPVOID pParam) { CString sComPort; sComPort = static_cast (pParam); return 1; } The declaration of the thread in the class header of CMyView is as below. UINT Thread_ScanConnectionState( LPVOID pParam ); I get the compilation error as given below error C2665: 'AfxBeginThread' : none of the 2 overloads can convert parameter 1 from type 'UINT (LPVOID)' c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\atlmfc\include\afxwin.h(4105): could be 'CWinThread *AfxBeginThread(AFX_THREADPROC,LPVOID,int,UINT,DWORD,LPSECURITY_ATTRIBUTES)' c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\atlmfc\include\afxwin.h(4108): or 'CWinThread *AfxBeginThread(CRuntimeClass *,int,UINT,DWORD,LPSECURITY_ATTRIBUTES)' while trying to match the argument list '(overloaded-function, CString)' Please let me know the error. laiju

    A 1 Reply Last reply
    0
    • L laiju

      Following is a funciton where I am calling a thread void CMyView::OnInitialUpdate() { ... AfxBeginThread(Thread_ScanConnectionState, m_sComPort, THREAD_PRIORITY_NORMAL, NULL, NULL, NULL); ... } //Foll is the thread definition UINT CMyView::Thread_ScanConnectionState(LPVOID pParam) { CString sComPort; sComPort = static_cast (pParam); return 1; } The declaration of the thread in the class header of CMyView is as below. UINT Thread_ScanConnectionState( LPVOID pParam ); I get the compilation error as given below error C2665: 'AfxBeginThread' : none of the 2 overloads can convert parameter 1 from type 'UINT (LPVOID)' c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\atlmfc\include\afxwin.h(4105): could be 'CWinThread *AfxBeginThread(AFX_THREADPROC,LPVOID,int,UINT,DWORD,LPSECURITY_ATTRIBUTES)' c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\atlmfc\include\afxwin.h(4108): or 'CWinThread *AfxBeginThread(CRuntimeClass *,int,UINT,DWORD,LPSECURITY_ATTRIBUTES)' while trying to match the argument list '(overloaded-function, CString)' Please let me know the error. laiju

      A Offline
      A Offline
      Arman S
      wrote on last edited by
      #2

      Hi, First: AfxBeginThread takes a static function pointer. Yours is not a static function. IOW, make Thread_ScanConnectionState static. Second: In the func nody you typed 'static_cast(pParam)': incorrect. The correct is: sComPort = static_cast (pParam); Third: Do not do extra typing: AfxBeginThread's default parameters work fine. You need not pass THREAD_PRIORITY_NORMAL or the NULLs.:) -- ====== Arman

      A L 2 Replies Last reply
      0
      • A Arman S

        Hi, First: AfxBeginThread takes a static function pointer. Yours is not a static function. IOW, make Thread_ScanConnectionState static. Second: In the func nody you typed 'static_cast(pParam)': incorrect. The correct is: sComPort = static_cast (pParam); Third: Do not do extra typing: AfxBeginThread's default parameters work fine. You need not pass THREAD_PRIORITY_NORMAL or the NULLs.:) -- ====== Arman

        A Offline
        A Offline
        Arman S
        wrote on last edited by
        #3

        Second: In the func nody you typed 'static_cast(pParam)': incorrect. The correct is: sComPort = static_cast (pParam); Sorry, it seems the editor doesn't love static_cast's signature:):-D:) -- ====== Arman

        T 1 Reply Last reply
        0
        • A Arman S

          Second: In the func nody you typed 'static_cast(pParam)': incorrect. The correct is: sComPort = static_cast (pParam); Sorry, it seems the editor doesn't love static_cast's signature:):-D:) -- ====== Arman

          T Offline
          T Offline
          toxcct
          wrote on last edited by
          #4

          Arman Z. Sahakyan wrote:

          Sorry, it seems the editor doesn't love static_cast's signature

          the editor is just html, so don't use html tags... <> are interpreted. if you don't want it to, check the Ignore HTML tags in this message (good for code snippets) checkbox or use &lt; &gt; codes instead...


          TOXCCT >>> GEII power
          [toxcct][VisualCalc 2.20][VisualCalc 3.0]

          A 1 Reply Last reply
          0
          • T toxcct

            Arman Z. Sahakyan wrote:

            Sorry, it seems the editor doesn't love static_cast's signature

            the editor is just html, so don't use html tags... <> are interpreted. if you don't want it to, check the Ignore HTML tags in this message (good for code snippets) checkbox or use &lt; &gt; codes instead...


            TOXCCT >>> GEII power
            [toxcct][VisualCalc 2.20][VisualCalc 3.0]

            A Offline
            A Offline
            Arman S
            wrote on last edited by
            #5

            Thanks. -- ====== Arman

            1 Reply Last reply
            0
            • A Arman S

              Hi, First: AfxBeginThread takes a static function pointer. Yours is not a static function. IOW, make Thread_ScanConnectionState static. Second: In the func nody you typed 'static_cast(pParam)': incorrect. The correct is: sComPort = static_cast (pParam); Third: Do not do extra typing: AfxBeginThread's default parameters work fine. You need not pass THREAD_PRIORITY_NORMAL or the NULLs.:) -- ====== Arman

              L Offline
              L Offline
              laiju
              wrote on last edited by
              #6

              Thanx for the info. But i didnt know that the THREAD func needs to be static.Thanx anyway laiju

              A T 2 Replies Last reply
              0
              • L laiju

                Thanx for the info. But i didnt know that the THREAD func needs to be static.Thanx anyway laiju

                A Offline
                A Offline
                Arman S
                wrote on last edited by
                #7

                Note that thread function must be either global or a static class-method. I wanted to say it cannot be a non-static class-method as you were using. -- ====== Arman

                1 Reply Last reply
                0
                • L laiju

                  Thanx for the info. But i didnt know that the THREAD func needs to be static.Thanx anyway laiju

                  T Offline
                  T Offline
                  toxcct
                  wrote on last edited by
                  #8

                  when a function is a class member (except static ones), it receives an implicit parameter which is the this pointer. this implicit parameter changes the signature of what you see of your function in design mode... for instance :

                  class A {
                  public:
                  void foo(int);
                  };

                  just considering the case of this function, its signature is in fact :

                  void A::foo(A*, int);

                  so, as AfxBeginThread() receives a function pointer, this parameter must implement a particuliar function signature, it must be exactly as you defined it. this is why your function must be global, or at least static (because static members don't know specific instances of a class - they are also known as class function)... hope that helps...


                  TOXCCT >>> GEII power
                  [toxcct][VisualCalc 2.20][VisualCalc 3.0]

                  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