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. Multithreaded App

Multithreaded App

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
9 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 Offline
    M Offline
    Manmohan29
    wrote on last edited by
    #1

    I am new to multithreading and my application doesnot compile. It is a Dialog Based Applicationin MFC with only one ButtonCtrl Here's the code

    UINT ThreadFunc(LPVOID pParam)
    {
    MessageBox("Thread Started");
    return 0;
    }

    void CAppDlg::OnButtonClkRunThread()
    {
    AfxBeginThread(ThreadFunc, this);
    }

    Where am I going wrong?

    Manmohan Bishnoi

    C G R 3 Replies Last reply
    0
    • M Manmohan29

      I am new to multithreading and my application doesnot compile. It is a Dialog Based Applicationin MFC with only one ButtonCtrl Here's the code

      UINT ThreadFunc(LPVOID pParam)
      {
      MessageBox("Thread Started");
      return 0;
      }

      void CAppDlg::OnButtonClkRunThread()
      {
      AfxBeginThread(ThreadFunc, this);
      }

      Where am I going wrong?

      Manmohan Bishnoi

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #2

      Usually compiler errors are pretty informative, so have a look at it. I guess the error occurs in

      Manmohan29 wrote:

      MessageBox("Thread Started");

      :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      1 Reply Last reply
      0
      • M Manmohan29

        I am new to multithreading and my application doesnot compile. It is a Dialog Based Applicationin MFC with only one ButtonCtrl Here's the code

        UINT ThreadFunc(LPVOID pParam)
        {
        MessageBox("Thread Started");
        return 0;
        }

        void CAppDlg::OnButtonClkRunThread()
        {
        AfxBeginThread(ThreadFunc, this);
        }

        Where am I going wrong?

        Manmohan Bishnoi

        G Offline
        G Offline
        Game point
        wrote on last edited by
        #3

        try this ...

        UINT ThreadFunc (LPVOID lpParam)
        {
        ::MessageBox(0,L"Thread Started",L"",1);
        return 0;
        }

        void CAppDlg::OnButtonClkRunThread()
        {
        ::AfxBeginThread (ThreadFunc,this);
        }

        1 Reply Last reply
        0
        • M Manmohan29

          I am new to multithreading and my application doesnot compile. It is a Dialog Based Applicationin MFC with only one ButtonCtrl Here's the code

          UINT ThreadFunc(LPVOID pParam)
          {
          MessageBox("Thread Started");
          return 0;
          }

          void CAppDlg::OnButtonClkRunThread()
          {
          AfxBeginThread(ThreadFunc, this);
          }

          Where am I going wrong?

          Manmohan Bishnoi

          R Offline
          R Offline
          Rajesh R Subramanian
          wrote on last edited by
          #4

          Manmohan29 wrote:

          doesnot compile

          And what does the compiler say? Be specific. To add to it,

          Manmohan29 wrote:

          UINT ThreadFunc(LPVOID pParam) { MessageBox("Thread Started"); //----> BAD idea! return 0; } void CAppDlg::OnButtonClkRunThread() { AfxBeginThread(ThreadFunc, this); }

          Because if your main thread becomes busy into something, the newly spawned thread as well would block for displaying the message. This is because you are using a function (MessageBox from MFC) that would make use of the default message pump of your application. In other words, if you add a Sleep(3000); after AfxBeginThread, the message from your thread would probably be displayed after this sleep period only! I see you're just starting to learn, but threading has such "gotchas", that you'll learn along the way. Lesson learned today: It isn't a great idea to use something within a worker thread, that may in turn use the default message pump of the application (in a blocking fashion). :)

          “Follow your bliss.” – Joseph Campbell

          M 1 Reply Last reply
          0
          • R Rajesh R Subramanian

            Manmohan29 wrote:

            doesnot compile

            And what does the compiler say? Be specific. To add to it,

            Manmohan29 wrote:

            UINT ThreadFunc(LPVOID pParam) { MessageBox("Thread Started"); //----> BAD idea! return 0; } void CAppDlg::OnButtonClkRunThread() { AfxBeginThread(ThreadFunc, this); }

            Because if your main thread becomes busy into something, the newly spawned thread as well would block for displaying the message. This is because you are using a function (MessageBox from MFC) that would make use of the default message pump of your application. In other words, if you add a Sleep(3000); after AfxBeginThread, the message from your thread would probably be displayed after this sleep period only! I see you're just starting to learn, but threading has such "gotchas", that you'll learn along the way. Lesson learned today: It isn't a great idea to use something within a worker thread, that may in turn use the default message pump of the application (in a blocking fashion). :)

            “Follow your bliss.” – Joseph Campbell

            M Offline
            M Offline
            Manmohan29
            wrote on last edited by
            #5

            NEW CODE :- UINT CThreadDlg::ThreadFunc(LPVOID pParam) { return 0; } void CThreadDlg::OnBnClickedBthread() { // TODO: Add your control notification handler code here AfxBeginThread(ThreadFunc, this); } ERROR :- ------ Build started: Project: Thread, Configuration: Debug Win32 ------ Compiling... ThreadDlg.cpp d:\manmohan\visual c++\thread\thread\threaddlg.cpp(162) : error C3867: 'CThreadDlg::ThreadFunc': function call missing argument list; use '&CThreadDlg::ThreadFunc' to create a pointer to member Build log was saved at "file://d:\Manmohan\Visual C++\Thread\Thread\Debug\BuildLog.htm" Thread - 1 error(s), 0 warning(s) ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

            Manmohan Bishnoi

            R R 3 Replies Last reply
            0
            • M Manmohan29

              NEW CODE :- UINT CThreadDlg::ThreadFunc(LPVOID pParam) { return 0; } void CThreadDlg::OnBnClickedBthread() { // TODO: Add your control notification handler code here AfxBeginThread(ThreadFunc, this); } ERROR :- ------ Build started: Project: Thread, Configuration: Debug Win32 ------ Compiling... ThreadDlg.cpp d:\manmohan\visual c++\thread\thread\threaddlg.cpp(162) : error C3867: 'CThreadDlg::ThreadFunc': function call missing argument list; use '&CThreadDlg::ThreadFunc' to create a pointer to member Build log was saved at "file://d:\Manmohan\Visual C++\Thread\Thread\Debug\BuildLog.htm" Thread - 1 error(s), 0 warning(s) ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

              Manmohan Bishnoi

              R Offline
              R Offline
              Rajesh R Subramanian
              wrote on last edited by
              #6

              Manmohan29 wrote:

              UINT CThreadDlg::ThreadFunc(LPVOID pParam) //---> Here is the problem! { return 0; } void CThreadDlg::OnBnClickedBthread() { // TODO: Add your control notification handler code here AfxBeginThread(ThreadFunc, this); }

              The thread function should be a global function (not a member function of any class), or it must be a static member function of any class. Try this instead: in your header file:

              static UINT CThreadDlg::ThreadFunc(LPVOID pParam);

              in the source file:

              /*static*/ UINT CThreadDlg::ThreadFunc(LPVOID pParam){
              OutputDebugString(_T("*********** Entered thread function ***********\n"));
              return false;
              }

              Watch the output window of your debugger to see the text printed from your thread.

              “Follow your bliss.” – Joseph Campbell

              1 Reply Last reply
              0
              • M Manmohan29

                NEW CODE :- UINT CThreadDlg::ThreadFunc(LPVOID pParam) { return 0; } void CThreadDlg::OnBnClickedBthread() { // TODO: Add your control notification handler code here AfxBeginThread(ThreadFunc, this); } ERROR :- ------ Build started: Project: Thread, Configuration: Debug Win32 ------ Compiling... ThreadDlg.cpp d:\manmohan\visual c++\thread\thread\threaddlg.cpp(162) : error C3867: 'CThreadDlg::ThreadFunc': function call missing argument list; use '&CThreadDlg::ThreadFunc' to create a pointer to member Build log was saved at "file://d:\Manmohan\Visual C++\Thread\Thread\Debug\BuildLog.htm" Thread - 1 error(s), 0 warning(s) ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

                Manmohan Bishnoi

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

                Manmohan29 wrote:

                UINT CThreadDlg::ThreadFunc(LPVOID pParam)

                This member has to be declared as 'static'.

                "It's supposed to be hard, otherwise anybody could do it!" - selfquote
                "High speed never compensates for wrong direction!" - unknown

                1 Reply Last reply
                0
                • M Manmohan29

                  NEW CODE :- UINT CThreadDlg::ThreadFunc(LPVOID pParam) { return 0; } void CThreadDlg::OnBnClickedBthread() { // TODO: Add your control notification handler code here AfxBeginThread(ThreadFunc, this); } ERROR :- ------ Build started: Project: Thread, Configuration: Debug Win32 ------ Compiling... ThreadDlg.cpp d:\manmohan\visual c++\thread\thread\threaddlg.cpp(162) : error C3867: 'CThreadDlg::ThreadFunc': function call missing argument list; use '&CThreadDlg::ThreadFunc' to create a pointer to member Build log was saved at "file://d:\Manmohan\Visual C++\Thread\Thread\Debug\BuildLog.htm" Thread - 1 error(s), 0 warning(s) ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

                  Manmohan Bishnoi

                  R Offline
                  R Offline
                  Rajesh R Subramanian
                  wrote on last edited by
                  #8

                  My previous answer to you is updated after you edited your post to include the code. Check it.

                  “Follow your bliss.” – Joseph Campbell

                  M 1 Reply Last reply
                  0
                  • R Rajesh R Subramanian

                    My previous answer to you is updated after you edited your post to include the code. Check it.

                    “Follow your bliss.” – Joseph Campbell

                    M Offline
                    M Offline
                    Manmohan29
                    wrote on last edited by
                    #9

                    Thanks, It works great.

                    Manmohan Bishnoi

                    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