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 pause processing?

How to pause processing?

Scheduled Pinned Locked Moved C / C++ / MFC
questiondata-structurestutorial
17 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.
  • D Dustin Henry

    That doesn't seem to work. Am I doing this correctly? I have never created/manipulated threads before.

    case WM_ACTIVATE:
    {
    	// See if we lost or gained focus
    	if (LOWORD(wParam) == WA_ACTIVE)
    	{
    		HANDLE thread = GetCurrentThread();
    		SetThreadPriority(thread, THREAD_PRIORITY_NORMAL);
    	}
    	else
    	{
    		HANDLE thread = GetCurrentThread();
    		SetThreadPriority(thread, THREAD_PRIORITY_LOWEST);
    	}
    }
    break;
    

    I also tried doing it on the WM_KILLFOCUS event but with the same results. Thanks, Dustin

    J Offline
    J Offline
    Johan Pretorius
    wrote on last edited by
    #4

    Something i forgot when you posted was that you wanted to pause the processing not lower it. try this if it does not work try send me an demo so i can test.

    //add global variable to main.cpp
    static bool g_bRunning = true;
    
    //Modify case statement
    case WM_ACTIVATE:
    {
       //See if we lost or gained focus
       if (LOWORD(wParam) == WA_ACTIVE)
       {
         g_bRunning = true;
       }
       else
       {
         g_bRunning = false;
       }
    }
    break;
    
    //add to main loop
    while (!g_bRunning){/*leave empty*/};//this should pause it.
    
    //just add an mutex and !!
    

    Artificial Intelligence is no match for Natural Stupidity
    No one can understand the truth until he drinks of coffee's frothy goodness. ~Sheik Abd-al-Kadir
    I can't always be wrong ... or can I?

    D 1 Reply Last reply
    0
    • D Dustin Henry

      I have an application with a main processing loop ( while(true){} ). When the application is running normally it eats up about 2% of the CPU. Whenever focus is taken away from the application window however, the CPU usage jumps to about 50%. This also happens when I comment out all of my code in the loop except the windows message queue processing and my app jumps from 60fps to about 2000fps. My question is, how do I 'pause' processing when focus is taken away, but still have the abilty to get window messages so I know when focus is given back? Thanks in advance, Dustin

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #5

      Dustin Henry wrote:

      I comment out all of my code in the loop except the windows message queue processing and my app jumps from 60fps to about 2000fps.

      What do you mean by "fps"? Typically a Windows message loop uses virtually no CPU until a message is received. What's going on in your message loop? Mark

      D J 2 Replies Last reply
      0
      • M Mark Salsbery

        Dustin Henry wrote:

        I comment out all of my code in the loop except the windows message queue processing and my app jumps from 60fps to about 2000fps.

        What do you mean by "fps"? Typically a Windows message loop uses virtually no CPU until a message is received. What's going on in your message loop? Mark

        D Offline
        D Offline
        Dustin Henry
        wrote on last edited by
        #6

        fps = Frames per second. It is a Direct3D application. Typically a Windows message loop uses virtually no CPU until a message is received I thought the same thing, but if I comment out all of my processing except for the message peek and dispatch, the app still eats the CPU when focus is lost. Here is my entire WindowProc:

        LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
        {	 
        	switch(msg)
        	{	
        		case WM_CREATE: 
        		{  
        			// do initialization stuff here
        			// return success
        			return(0);
        		} 
        		break;
        		case WM_LBUTTONDOWN:
        		{
        			// get the position of the mouse
        			POINT ptMouse = {(int)LOWORD(lParam),(int)HIWORD(lParam)};
        			g_pInterface->UpdateMouse(ptMouse, true);
        		}
        		break;
        		case WM_LBUTTONUP:
        		{
        			// get the position of the mouse
        			POINT ptMouse = {(int)LOWORD(lParam),(int)HIWORD(lParam)};
        			g_pInterface->UpdateMouse(ptMouse, false);
        		}
        		break;
        		case WM_MOUSEMOVE:
        		{
        			// get the position of the mouse
        			POINT ptMouse = {(int)LOWORD(lParam),(int)HIWORD(lParam)};
        			g_pInterface->UpdateMouse(ptMouse);
        
        			CString stMove;
        			stMove.Format("(%d,%d)",(int)LOWORD(lParam),(int)HIWORD(lParam));
        			//SetWindowText(g_hWND, stMove);
        		}
        		break;
        		case WM_DESTROY: 
        		{
        			// kill the application, this sends a WM_QUIT message 
        			PostQuitMessage(0);
        			// return success
        			return(0);
        		} 
        		break;
        		case WM_GRAPHNOTIFY:
        		{
        			g_pInterface->GetDirectShow()->OnGraphEvent();
        		}
        		break;
        		default:break;
        
        	} // end switch
        	// process any messages that we didn't take care of 
        	return (DefWindowProc(hWnd, msg, wParam, lParam));
        } // end WinProc
        

        And my main loop: (the message part)

        while(TRUE)
        {
        	// Get the time this function began executing
        	//DWORD dwStartTime = GetTickCount();
        	// test if there is a message in queue, if so get it
        	if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
        	{ 
        		// test if this is a quit
        		if (msg.message == WM_QUIT)
        		   break;
        		// translate any accelerator keys
        		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        		{
        			TranslateMessage(&msg);
        			// send the message to the window proc
        			DispatchMessage(&msg);
        		}
        	} // end if
        /* Other processing... */
        }
        

        Dustin

        M 1 Reply Last reply
        0
        • J Johan Pretorius

          Something i forgot when you posted was that you wanted to pause the processing not lower it. try this if it does not work try send me an demo so i can test.

          //add global variable to main.cpp
          static bool g_bRunning = true;
          
          //Modify case statement
          case WM_ACTIVATE:
          {
             //See if we lost or gained focus
             if (LOWORD(wParam) == WA_ACTIVE)
             {
               g_bRunning = true;
             }
             else
             {
               g_bRunning = false;
             }
          }
          break;
          
          //add to main loop
          while (!g_bRunning){/*leave empty*/};//this should pause it.
          
          //just add an mutex and !!
          

          Artificial Intelligence is no match for Natural Stupidity
          No one can understand the truth until he drinks of coffee's frothy goodness. ~Sheik Abd-al-Kadir
          I can't always be wrong ... or can I?

          D Offline
          D Offline
          Dustin Henry
          wrote on last edited by
          #7

          Since my message peek is in the main loop, if I do that it will no longer look for messages and will not tell me when focus has been restored.

          J 1 Reply Last reply
          0
          • M Mark Salsbery

            Dustin Henry wrote:

            I comment out all of my code in the loop except the windows message queue processing and my app jumps from 60fps to about 2000fps.

            What do you mean by "fps"? Typically a Windows message loop uses virtually no CPU until a message is received. What's going on in your message loop? Mark

            J Offline
            J Offline
            Johan Pretorius
            wrote on last edited by
            #8

            Mark Salsbery wrote:

            What do you mean by "fps"?

            Well i am guessing he is writing a game/game engine. Applications also lose focus when they are minimized. That is why he probaly needs the application to stop processing.


            Artificial Intelligence is no match for Natural Stupidity
            No one can understand the truth until he drinks of coffee's frothy goodness. ~Sheik Abd-al-Kadir
            I can't always be wrong ... or can I?

            J 1 Reply Last reply
            0
            • D Dustin Henry

              Since my message peek is in the main loop, if I do that it will no longer look for messages and will not tell me when focus has been restored.

              J Offline
              J Offline
              Johan Pretorius
              wrote on last edited by
              #9

              Well you could put a copy of the messagepeek functions to the while (!m_bRunning) {}; then there should be no problems


              Artificial Intelligence is no match for Natural Stupidity
              No one can understand the truth until he drinks of coffee's frothy goodness. ~Sheik Abd-al-Kadir
              I can't always be wrong ... or can I?

              1 Reply Last reply
              0
              • J Johan Pretorius

                Mark Salsbery wrote:

                What do you mean by "fps"?

                Well i am guessing he is writing a game/game engine. Applications also lose focus when they are minimized. That is why he probaly needs the application to stop processing.


                Artificial Intelligence is no match for Natural Stupidity
                No one can understand the truth until he drinks of coffee's frothy goodness. ~Sheik Abd-al-Kadir
                I can't always be wrong ... or can I?

                J Offline
                J Offline
                Johan Pretorius
                wrote on last edited by
                #10

                Sorry his reply was not there when i posted mine


                Artificial Intelligence is no match for Natural Stupidity
                No one can understand the truth until he drinks of coffee's frothy goodness. ~Sheik Abd-al-Kadir
                I can't always be wrong ... or can I?

                D 1 Reply Last reply
                0
                • J Johan Pretorius

                  Sorry his reply was not there when i posted mine


                  Artificial Intelligence is no match for Natural Stupidity
                  No one can understand the truth until he drinks of coffee's frothy goodness. ~Sheik Abd-al-Kadir
                  I can't always be wrong ... or can I?

                  D Offline
                  D Offline
                  Dustin Henry
                  wrote on last edited by
                  #11

                  Well I think I fixed it. I basically used CaveFox's idea of a running boolean but in the main loop placed:

                  if (!g_bRunning)
                  	Sleep(1000);
                  

                  I believe this was happening because I allow the program to basically sit and spin in an infinite loop. Even if I commented out everything in the while(), the same thing happened. I guess the program just needed something to do. Any idea why this is? Thanks for your help guys. Dustin

                  M J 2 Replies Last reply
                  0
                  • D Dustin Henry

                    fps = Frames per second. It is a Direct3D application. Typically a Windows message loop uses virtually no CPU until a message is received I thought the same thing, but if I comment out all of my processing except for the message peek and dispatch, the app still eats the CPU when focus is lost. Here is my entire WindowProc:

                    LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
                    {	 
                    	switch(msg)
                    	{	
                    		case WM_CREATE: 
                    		{  
                    			// do initialization stuff here
                    			// return success
                    			return(0);
                    		} 
                    		break;
                    		case WM_LBUTTONDOWN:
                    		{
                    			// get the position of the mouse
                    			POINT ptMouse = {(int)LOWORD(lParam),(int)HIWORD(lParam)};
                    			g_pInterface->UpdateMouse(ptMouse, true);
                    		}
                    		break;
                    		case WM_LBUTTONUP:
                    		{
                    			// get the position of the mouse
                    			POINT ptMouse = {(int)LOWORD(lParam),(int)HIWORD(lParam)};
                    			g_pInterface->UpdateMouse(ptMouse, false);
                    		}
                    		break;
                    		case WM_MOUSEMOVE:
                    		{
                    			// get the position of the mouse
                    			POINT ptMouse = {(int)LOWORD(lParam),(int)HIWORD(lParam)};
                    			g_pInterface->UpdateMouse(ptMouse);
                    
                    			CString stMove;
                    			stMove.Format("(%d,%d)",(int)LOWORD(lParam),(int)HIWORD(lParam));
                    			//SetWindowText(g_hWND, stMove);
                    		}
                    		break;
                    		case WM_DESTROY: 
                    		{
                    			// kill the application, this sends a WM_QUIT message 
                    			PostQuitMessage(0);
                    			// return success
                    			return(0);
                    		} 
                    		break;
                    		case WM_GRAPHNOTIFY:
                    		{
                    			g_pInterface->GetDirectShow()->OnGraphEvent();
                    		}
                    		break;
                    		default:break;
                    
                    	} // end switch
                    	// process any messages that we didn't take care of 
                    	return (DefWindowProc(hWnd, msg, wParam, lParam));
                    } // end WinProc
                    

                    And my main loop: (the message part)

                    while(TRUE)
                    {
                    	// Get the time this function began executing
                    	//DWORD dwStartTime = GetTickCount();
                    	// test if there is a message in queue, if so get it
                    	if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
                    	{ 
                    		// test if this is a quit
                    		if (msg.message == WM_QUIT)
                    		   break;
                    		// translate any accelerator keys
                    		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
                    		{
                    			TranslateMessage(&msg);
                    			// send the message to the window proc
                    			DispatchMessage(&msg);
                    		}
                    	} // end if
                    /* Other processing... */
                    }
                    

                    Dustin

                    M Offline
                    M Offline
                    Mark Salsbery
                    wrote on last edited by
                    #12

                    Try GetMessage instead of PeekMessage You shouldn't have to sleep or mess with thread priority for user interface thread :) Mark

                    1 Reply Last reply
                    0
                    • D Dustin Henry

                      Well I think I fixed it. I basically used CaveFox's idea of a running boolean but in the main loop placed:

                      if (!g_bRunning)
                      	Sleep(1000);
                      

                      I believe this was happening because I allow the program to basically sit and spin in an infinite loop. Even if I commented out everything in the while(), the same thing happened. I guess the program just needed something to do. Any idea why this is? Thanks for your help guys. Dustin

                      M Offline
                      M Offline
                      Mark Salsbery
                      wrote on last edited by
                      #13

                      No SLEEP()!!! ;)

                      while(GetMessage(&msg,NULL,0,0))
                      {
                      // translate any accelerator keys
                      if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
                      {
                      TranslateMessage(&msg);
                      // send the message to the window proc
                      DispatchMessage(&msg);
                      }
                      }

                      1 Reply Last reply
                      0
                      • D Dustin Henry

                        Well I think I fixed it. I basically used CaveFox's idea of a running boolean but in the main loop placed:

                        if (!g_bRunning)
                        	Sleep(1000);
                        

                        I believe this was happening because I allow the program to basically sit and spin in an infinite loop. Even if I commented out everything in the while(), the same thing happened. I guess the program just needed something to do. Any idea why this is? Thanks for your help guys. Dustin

                        J Offline
                        J Offline
                        Johan Pretorius
                        wrote on last edited by
                        #14

                        This should stop all processing (except the messages).

                        case WM_ACTIVATE:
                        {
                           //See if we lost or gained focus
                           if (LOWORD(wParam) == WA_ACTIVE)
                           {
                             g_bRunning = true;
                           }
                           else
                           {
                             g_bRunning = false;
                           }
                        }
                        break;
                        
                        
                        static bool m_bRunning = true;
                        
                        while(TRUE)
                        {
                        	// Get the time this function began executing
                        	//DWORD dwStartTime = GetTickCount();
                        	// test if there is a message in queue, if so get it
                                do
                                {
                                     if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
                                     { 
                        		// test if this is a quit
                        		if (msg.message == WM_QUIT)
                        		   break;
                        		// translate any accelerator keys
                        		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
                        		{
                        			TranslateMessage(&msg);
                        			// send the message to the window proc
                        			DispatchMessage(&msg);
                        		}
                        	     } // end if
                                } while(!m_bRunning)
                         /* Other processing... */
                        }
                        

                        Artificial Intelligence is no match for Natural Stupidity
                        No one can understand the truth until he drinks of coffee's frothy goodness. ~Sheik Abd-al-Kadir
                        I can't always be wrong ... or can I?

                        1 Reply Last reply
                        0
                        • D Dustin Henry

                          I have an application with a main processing loop ( while(true){} ). When the application is running normally it eats up about 2% of the CPU. Whenever focus is taken away from the application window however, the CPU usage jumps to about 50%. This also happens when I comment out all of my code in the loop except the windows message queue processing and my app jumps from 60fps to about 2000fps. My question is, how do I 'pause' processing when focus is taken away, but still have the abilty to get window messages so I know when focus is given back? Thanks in advance, Dustin

                          M Offline
                          M Offline
                          Mark Salsbery
                          wrote on last edited by
                          #15

                          Really, I BEG you guys...pleeeease leave the poor UI thread alone :(( What did it ever do to you??? :)

                          J 1 Reply Last reply
                          0
                          • M Mark Salsbery

                            Really, I BEG you guys...pleeeease leave the poor UI thread alone :(( What did it ever do to you??? :)

                            J Offline
                            J Offline
                            Johan Pretorius
                            wrote on last edited by
                            #16

                            Yea it was a little overkill (by me X|) ... im sleep deprived and are not thinking strait. Should have solved it with the first post :^)


                            Artificial Intelligence is no match for Natural Stupidity
                            No one can understand the truth until he drinks of coffee's frothy goodness. ~Sheik Abd-al-Kadir
                            I can't always be wrong ... or can I?

                            M 1 Reply Last reply
                            0
                            • J Johan Pretorius

                              Yea it was a little overkill (by me X|) ... im sleep deprived and are not thinking strait. Should have solved it with the first post :^)


                              Artificial Intelligence is no match for Natural Stupidity
                              No one can understand the truth until he drinks of coffee's frothy goodness. ~Sheik Abd-al-Kadir
                              I can't always be wrong ... or can I?

                              M Offline
                              M Offline
                              Mark Salsbery
                              wrote on last edited by
                              #17

                              :laugh: It looked right to me - I had to test it before I saw it! As soon as Sleep() comes up, it's time for an intervention :)

                              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