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 write a function that calls a secondary thread and returns back to the main thread?

How to write a function that calls a secondary thread and returns back to the main thread?

Scheduled Pinned Locked Moved C / C++ / MFC
questionarchitecturetutorial
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.
  • E Offline
    E Offline
    eight
    wrote on last edited by
    #1

    Hi This is more of a code architecture question. I'll try my best to explain what I'm trying to do. I need to create a function that have access to a secondary thread, process it, and returns back to the primary thread. Let say class B has FunctionX.

    bool B::FunctionX(LPCTSTR lpszValue, CString& strResponse)
    {
    //Access the secondary thread to process lpszValue and assign strResponse;
    //Returns true if process successful and false if process fail
    }

    Then, class CMyView will call FunctionX like so

    void CMyView::Foo()
    {
    B myB;
    CString str;
    if(myB.FunctionX(TEXT("TestValue"), str)
    {
    AfxMessagebox(TEXT("Oh Yeah!"));
    }
    else
    {
    AfxMessageBox(TEXT("Boohooo!"));
    }
    }

    A C 2 Replies Last reply
    0
    • E eight

      Hi This is more of a code architecture question. I'll try my best to explain what I'm trying to do. I need to create a function that have access to a secondary thread, process it, and returns back to the primary thread. Let say class B has FunctionX.

      bool B::FunctionX(LPCTSTR lpszValue, CString& strResponse)
      {
      //Access the secondary thread to process lpszValue and assign strResponse;
      //Returns true if process successful and false if process fail
      }

      Then, class CMyView will call FunctionX like so

      void CMyView::Foo()
      {
      B myB;
      CString str;
      if(myB.FunctionX(TEXT("TestValue"), str)
      {
      AfxMessagebox(TEXT("Oh Yeah!"));
      }
      else
      {
      AfxMessageBox(TEXT("Boohooo!"));
      }
      }

      A Offline
      A Offline
      Adam Roderick J
      wrote on last edited by
      #2

      So u mean you want to create a thread(Secondary) inside a thread(primary) and then do the secondary processing and then continue the primary.

      Величие не Бога может быть недооценена.

      1 Reply Last reply
      0
      • E eight

        Hi This is more of a code architecture question. I'll try my best to explain what I'm trying to do. I need to create a function that have access to a secondary thread, process it, and returns back to the primary thread. Let say class B has FunctionX.

        bool B::FunctionX(LPCTSTR lpszValue, CString& strResponse)
        {
        //Access the secondary thread to process lpszValue and assign strResponse;
        //Returns true if process successful and false if process fail
        }

        Then, class CMyView will call FunctionX like so

        void CMyView::Foo()
        {
        B myB;
        CString str;
        if(myB.FunctionX(TEXT("TestValue"), str)
        {
        AfxMessagebox(TEXT("Oh Yeah!"));
        }
        else
        {
        AfxMessageBox(TEXT("Boohooo!"));
        }
        }

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

        I think you didn't really understand how threading works. You can't "call" a thread, a thread is running in parallel of your main thread and you cannot call it. From your example, it looks like you want to do some processing, get the results of the processing when done and display that result, all of that WITHOUT freezing the UI (that's why you want a thread, right) ? Am I correct ? If yes, then this is not the way to do it: in your Foo function, you are anyway waiting for the result of your thread (thus blocking the main thread), which means that the UI messages won't be processed anymore. The best way to solve your problem is to start the thread (in your FunctionX for instance) and then post a user defined message containing the result to the UI. I suggest you read this very good article[^] for more information.

        Cédric Moonen Software developer
        Charting control [v3.0] OpenGL game tutorial in C++

        E 1 Reply Last reply
        0
        • C Cedric Moonen

          I think you didn't really understand how threading works. You can't "call" a thread, a thread is running in parallel of your main thread and you cannot call it. From your example, it looks like you want to do some processing, get the results of the processing when done and display that result, all of that WITHOUT freezing the UI (that's why you want a thread, right) ? Am I correct ? If yes, then this is not the way to do it: in your Foo function, you are anyway waiting for the result of your thread (thus blocking the main thread), which means that the UI messages won't be processed anymore. The best way to solve your problem is to start the thread (in your FunctionX for instance) and then post a user defined message containing the result to the UI. I suggest you read this very good article[^] for more information.

          Cédric Moonen Software developer
          Charting control [v3.0] OpenGL game tutorial in C++

          E Offline
          E Offline
          eight
          wrote on last edited by
          #4

          Thanks for the link. Helps a lot :)

          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