Cycle

This is a discussion on Cycle within the Windows Programming forums, part of the Platform Specific Boards category; Hi, It is not so much an issue, I am rather wondering if the program work correctly... Code: int iTickCount ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    162

    Cycle

    Hi,

    It is not so much an issue, I am rather wondering if the program work correctly...

    Code:
    int iTickCount = 0;
    int iTickTriger = 0;
    
    while(TRUE)
    	{
    		if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
    		{
    		      TranslateMessage(&msg);
    		      DispatchMessage(&msg);
    		}
    		else
    		{
    			iTickCount = GetTickCount();
    			if(iTickCount > iTickTriger)
    			{
    				iTickTriger = iTickCount + 50; // set the frame rate (20fps)
    				ProgCycle(hWnd, hDC); // Function of the main program run
    			}
    		}
    	}
    (The code is supposed to create an infinite loop of the programme which runs at 20 fps)
    [This code is copied from a learners book of some little games and application and obviously works well]

    The thing I am refering to is that this loop makes the CPU run on 100%... It does not slow down the computer, all other applications run at ussual speed but the CPU usage is about 100% all the time while running this app...

    I am wondering if it is correct sollution. And if not what else shall I try istead???

    Thank you in advance

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    If it's for a game, using the CPU up is usually fine, though you might want to sleep a bit if you have a lot of time to spare.

    For other apps, using a timer for periodic stuff would be more appropriate.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Personally I hate programs that use 100% of the CPU. It's really not that difficult to give up your extra time to other processes.

    Here's some SDL code that does it (though if you're using the SDL you can use the framerate manager from SDL_gfx):
    Code:
    Uint32 start, end;
    start = SDL_GetTicks();  /* record starting time */
    
    /* ... do processor-intensive stuff ... */
    
    end = SDL_GetTickS();
    if(end-start < 1000/20.0) {  /* if the time taken was less than one twentieth of a second */
        SDL_Delay(1000/20.0 - (end-start));  /* delay the remaining time */
    }
    I've posted information on this before, search around if you're interested . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    162
    Ok... Thanks for hints...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multigrid V cycle for PDE solver
    By lagnajita in forum C Programming
    Replies: 0
    Last Post: 03-06-2009, 03:40 AM
  2. life cycle of exception object
    By George2 in forum C++ Programming
    Replies: 43
    Last Post: 02-13-2008, 07:50 AM
  3. Fidning a cycle in a directed graph.
    By apacz in forum C++ Programming
    Replies: 9
    Last Post: 10-31-2005, 04:22 PM
  4. Euler Cycle
    By nickwokabi in forum C++ Programming
    Replies: 2
    Last Post: 07-20-2005, 10:54 AM
  5. Understanding the Window WinMain Cycle
    By BillBoeBaggins in forum Windows Programming
    Replies: 4
    Last Post: 12-06-2003, 12:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21