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

AfxBeginThread

Scheduled Pinned Locked Moved C / C++ / MFC
question
12 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.
  • T Offline
    T Offline
    tanarnelinistit
    wrote on last edited by
    #1

    Can I pass a parameter to the function if I have a worker thread and use AfxBeginThread?

    C H 2 Replies Last reply
    0
    • T tanarnelinistit

      Can I pass a parameter to the function if I have a worker thread and use AfxBeginThread?

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

      From MSDN[^]:

      CWinThread* AfxBeginThread(
      AFX_THREADPROC pfnThreadProc,
      LPVOID pParam,
      int nPriority = THREAD_PRIORITY_NORMAL,
      UINT nStackSize = 0,
      DWORD dwCreateFlags = 0,
      LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL
      );

      pParam Parameter to be passed to the controlling function as shown in the parameter to the function declaration in pfnThreadProc.


      Cédric Moonen Software developer
      Charting control

      T 1 Reply Last reply
      0
      • T tanarnelinistit

        Can I pass a parameter to the function if I have a worker thread and use AfxBeginThread?

        H Offline
        H Offline
        Hamid Taebi
        wrote on last edited by
        #3

        See Here[^] maybe it is some helpful to you

        _**


        **_

        WhiteSky


        1 Reply Last reply
        0
        • C Cedric Moonen

          From MSDN[^]:

          CWinThread* AfxBeginThread(
          AFX_THREADPROC pfnThreadProc,
          LPVOID pParam,
          int nPriority = THREAD_PRIORITY_NORMAL,
          UINT nStackSize = 0,
          DWORD dwCreateFlags = 0,
          LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL
          );

          pParam Parameter to be passed to the controlling function as shown in the parameter to the function declaration in pfnThreadProc.


          Cédric Moonen Software developer
          Charting control

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

          I've also read that but I still don't understand??!!?

          C 1 Reply Last reply
          0
          • T tanarnelinistit

            I've also read that but I still don't understand??!!?

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

            Then ask a clear question. What is your problem ?? We cannot guess it. Why don't you just pass your data (as a pointer for example) using this parameter ?


            Cédric Moonen Software developer
            Charting control

            T 1 Reply Last reply
            0
            • C Cedric Moonen

              Then ask a clear question. What is your problem ?? We cannot guess it. Why don't you just pass your data (as a pointer for example) using this parameter ?


              Cédric Moonen Software developer
              Charting control

              T Offline
              T Offline
              tanarnelinistit
              wrote on last edited by
              #6

              This is my controling function UINT CMina_sView::run(LPVOID p) { CMina_sView * me = (CMina_sView *)p; me->ascultarea(); return 0; } And this is how I start the thread void CMina_sView::OnList() { running = TRUE; AfxBeginThread(run, this); } I want to pass a paramater to ascultarea. How do i do it?

              C 1 Reply Last reply
              0
              • T tanarnelinistit

                This is my controling function UINT CMina_sView::run(LPVOID p) { CMina_sView * me = (CMina_sView *)p; me->ascultarea(); return 0; } And this is how I start the thread void CMina_sView::OnList() { running = TRUE; AfxBeginThread(run, this); } I want to pass a paramater to ascultarea. How do i do it?

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

                You already passed a parameter to your thread: it's the address of the class (the this parameter). So, you cannot pass two parameters. But, why do you want to pass a parameter to this function ? Why don't you just have a member variable that hold this variable so that the function ascultarea can use it ?


                Cédric Moonen Software developer
                Charting control

                T 1 Reply Last reply
                0
                • C Cedric Moonen

                  You already passed a parameter to your thread: it's the address of the class (the this parameter). So, you cannot pass two parameters. But, why do you want to pass a parameter to this function ? Why don't you just have a member variable that hold this variable so that the function ascultarea can use it ?


                  Cédric Moonen Software developer
                  Charting control

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

                  The OnList is a menu item that opens a dialog in which I can select smth a i hold that in dlg.m_sel. I want to be able to pass dlg.m_sel to ascultarea.

                  C 1 Reply Last reply
                  0
                  • T tanarnelinistit

                    The OnList is a menu item that opens a dialog in which I can select smth a i hold that in dlg.m_sel. I want to be able to pass dlg.m_sel to ascultarea.

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

                    tanarnelinistit wrote:

                    I can select smth a i hold that in dlg.m_sel

                    :confused: Please make a little effort on the spelling because I cannot understand (english is not my mother tongue). When are you opening the dialog ? Before or after spawning the thread ? If it is after, then why don't you simply take this value (dlg.m_sel) and store it into a member variable of CMina_sView ? Then in your thread, as you call the function ascultarea, as it is inside the SAME class, you don't need to pass it to the function. The function can simply get it from the member variable. I think the problem is that you don't know what you are doing. You probably found some snippet of code to start the thread but you don't understand the mechanics beyond that. Am I right ? Then, I cannot explain you in details because the subject is fairly too large. (And I already provided you with the solution).


                    Cédric Moonen Software developer
                    Charting control

                    T 1 Reply Last reply
                    0
                    • C Cedric Moonen

                      tanarnelinistit wrote:

                      I can select smth a i hold that in dlg.m_sel

                      :confused: Please make a little effort on the spelling because I cannot understand (english is not my mother tongue). When are you opening the dialog ? Before or after spawning the thread ? If it is after, then why don't you simply take this value (dlg.m_sel) and store it into a member variable of CMina_sView ? Then in your thread, as you call the function ascultarea, as it is inside the SAME class, you don't need to pass it to the function. The function can simply get it from the member variable. I think the problem is that you don't know what you are doing. You probably found some snippet of code to start the thread but you don't understand the mechanics beyond that. Am I right ? Then, I cannot explain you in details because the subject is fairly too large. (And I already provided you with the solution).


                      Cédric Moonen Software developer
                      Charting control

                      T Offline
                      T Offline
                      tanarnelinistit
                      wrote on last edited by
                      #10

                      1)I knew I should've studied french a little more in highschool. 2)I started programing in VC++ just one month ago but I've read and understood anything I could in one month. 3) I open the dialog and then I spawn the thread.

                      C 1 Reply Last reply
                      0
                      • T tanarnelinistit

                        1)I knew I should've studied french a little more in highschool. 2)I started programing in VC++ just one month ago but I've read and understood anything I could in one month. 3) I open the dialog and then I spawn the thread.

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

                        I already explained you the solution 2 times. Once more won't help you to understand it better. After one month of studying C++, it is not a good idea to jump directly to thread manipulation (even more if you didn't know a programming language before). The only advice I can give you is: start slowly. Try to find a good book or course and start to learn the basics first. Otherwise, you'll screw up everything and you'll be really disgusted for life.


                        Cédric Moonen Software developer
                        Charting control

                        T 1 Reply Last reply
                        0
                        • C Cedric Moonen

                          I already explained you the solution 2 times. Once more won't help you to understand it better. After one month of studying C++, it is not a good idea to jump directly to thread manipulation (even more if you didn't know a programming language before). The only advice I can give you is: start slowly. Try to find a good book or course and start to learn the basics first. Otherwise, you'll screw up everything and you'll be really disgusted for life.


                          Cédric Moonen Software developer
                          Charting control

                          T Offline
                          T Offline
                          tanarnelinistit
                          wrote on last edited by
                          #12

                          Ok. Thanks a lot. Although I feel a little bit disapointed now, I got your point.

                          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