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. stop main thread and resume later

stop main thread and resume later

Scheduled Pinned Locked Moved C / C++ / MFC
designhelpquestioncareer
9 Posts 4 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.
  • _ Offline
    _ Offline
    _T No name
    wrote on last edited by
    #1

    hi everyone. in my prog i kinnda need to stop my main thread until other thread is running. can nyone plz help me to tell if this is fine with design point of view or at all possible. If its possible plz tell me how.. using CEvent how can i creat event signalled for main thread thank

    S C 2 Replies Last reply
    0
    • _ _T No name

      hi everyone. in my prog i kinnda need to stop my main thread until other thread is running. can nyone plz help me to tell if this is fine with design point of view or at all possible. If its possible plz tell me how.. using CEvent how can i creat event signalled for main thread thank

      S Offline
      S Offline
      ShiXiangYang
      wrote on last edited by
      #2

      In my think if you stop the main thread the procedure will terminate. Maybe you can create a loop in main thread to wait the event that you hope.

      _ C 2 Replies Last reply
      0
      • S ShiXiangYang

        In my think if you stop the main thread the procedure will terminate. Maybe you can create a loop in main thread to wait the event that you hope.

        _ Offline
        _ Offline
        _T No name
        wrote on last edited by
        #3

        thanx for ur reply thats wt i want.... but how to create an event for main thread and loop it ?

        W 1 Reply Last reply
        0
        • _ _T No name

          hi everyone. in my prog i kinnda need to stop my main thread until other thread is running. can nyone plz help me to tell if this is fine with design point of view or at all possible. If its possible plz tell me how.. using CEvent how can i creat event signalled for main thread thank

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

          Which kind of application are you working on ? If it is a win32 or MFC application, I strongly advice not to stop the main thread because no messages will be processed anymore and your UI will hang. If it is a console application and you want to wait until something happens, then using an event is one of the way to do it: simply call WaitForSingleObject[^] which will wait until the event you passed to handle is signaled. To crate the event, call CreateEvent[^]. In your separate thread, once you need to signal the main thread, call SetEvent[^], which will set the event and end the wait in the main thread. These are the win32 functions, there are also MFC versions if you need.

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

          _ 2 Replies Last reply
          0
          • S ShiXiangYang

            In my think if you stop the main thread the procedure will terminate. Maybe you can create a loop in main thread to wait the event that you hope.

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

            ShiXiangYang wrote:

            Maybe you can create a loop in main thread to wait the event that you hope.

            You mean busy waiting (a simple loop that continuously checks the state of a certain variable) ? That's pretty ugly because it will consume CPU cycles for nothing. Better to use synchronization objects (events for instance).

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

            1 Reply Last reply
            0
            • C Cedric Moonen

              Which kind of application are you working on ? If it is a win32 or MFC application, I strongly advice not to stop the main thread because no messages will be processed anymore and your UI will hang. If it is a console application and you want to wait until something happens, then using an event is one of the way to do it: simply call WaitForSingleObject[^] which will wait until the event you passed to handle is signaled. To crate the event, call CreateEvent[^]. In your separate thread, once you need to signal the main thread, call SetEvent[^], which will set the event and end the wait in the main thread. These are the win32 functions, there are also MFC versions if you need.

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

              _ Offline
              _ Offline
              _T No name
              wrote on last edited by
              #6

              thanx i think this is what i was looking for i will try this and let u knw thanx anyways

              1 Reply Last reply
              0
              • _ _T No name

                thanx for ur reply thats wt i want.... but how to create an event for main thread and loop it ?

                W Offline
                W Offline
                wo will rock you
                wrote on last edited by
                #7

                you can use "WaitForSingleObject" function.

                I can read a word.

                1 Reply Last reply
                0
                • C Cedric Moonen

                  Which kind of application are you working on ? If it is a win32 or MFC application, I strongly advice not to stop the main thread because no messages will be processed anymore and your UI will hang. If it is a console application and you want to wait until something happens, then using an event is one of the way to do it: simply call WaitForSingleObject[^] which will wait until the event you passed to handle is signaled. To crate the event, call CreateEvent[^]. In your separate thread, once you need to signal the main thread, call SetEvent[^], which will set the event and end the wait in the main thread. These are the win32 functions, there are also MFC versions if you need.

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

                  _ Offline
                  _ Offline
                  _T No name
                  wrote on last edited by
                  #8

                  hello i tried the method u told me .. but its kindda not working for me. i created event in Constructor (main thread) intinally reset manualset true ::SetEvent[^] & ::WaitFOrSingleObject[^] before AfxBeginThread() in threadProcFunction i called ::ResetEvent[^] actually mine is GUI application (MFC) m able to stop all mouse and keyboard event by doing modelling of parent. But in maximize and minimize view is being drawn again and again that i dnt want. thats why i want to wait my mainthread for a while. till worker thread does its work plz help thankx

                  C 1 Reply Last reply
                  0
                  • _ _T No name

                    hello i tried the method u told me .. but its kindda not working for me. i created event in Constructor (main thread) intinally reset manualset true ::SetEvent[^] & ::WaitFOrSingleObject[^] before AfxBeginThread() in threadProcFunction i called ::ResetEvent[^] actually mine is GUI application (MFC) m able to stop all mouse and keyboard event by doing modelling of parent. But in maximize and minimize view is being drawn again and again that i dnt want. thats why i want to wait my mainthread for a while. till worker thread does its work plz help thankx

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

                    I told you that it was no a good idea to use this technique in an MFC application.

                    _T("No name") wrote:

                    But in maximize and minimize view is being drawn again and again that i dnt want. thats why i want to wait my mainthread for a while. till worker thread does its work

                    What do you mean by again and again ? Normally, in an MFC application, the window is only repainted on request (when a WM_PAINT message is sent). So if it is continuously repainting, you are doing something for that. If you want to stop the repainting, just stop doing that thing you are doing :) (sorry, can't be more accurate without seing any code). And please, try to avoid txtspk and use correct phrase structures, your question will be much more understandable.

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

                    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