CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Oct 2007
    Posts
    5

    Program Main Loop.

    Hi all,

    How does one make a main loop in a c++ program? Is there any secret on it?

    I've done the very simple code below and it is taking 50% of CPU Usage, so, is there any thing i should do to slow up stuf?

    Regards

    Code:
    int main(){
    
    while(1==1){
    
    }
    
    }

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: Program Main Loop.

    Why worry about a mainloop ?? write the program you want and if you need a loop you will know by then.

  3. #3
    Join Date
    Sep 2005
    Posts
    7

    Re: Program Main Loop.

    If you are using VC++ and if you want your loop to run at given time intervals, you can use the WM_TIMER message. Search MSDN for it.

  4. #4
    Join Date
    Jun 2007
    Posts
    44

    Re: Program Main Loop.

    Use Sleep. Sleep(10) to sleep 10 milliseconds, which will suspend your thread for 10ms so the CPU can do other things.

    If you're writing a traditional windows message loop, use PeekMessage() to check for pending messages, then again take a nap with Sleep() when you're done. You can then control the temporal granularity (is that a proper term?) of your processing loops.

    Of course, if you're writing a game program, it's pretty typical for it to eat up 100% of the CPU, as it is a performance-based application and should always have something to do.

    WM_TIMER messages is very easy to use but is not in my experience terribly consistent under 2-3 seconds, especially as you have to get around to processing the message(s) first. If you have something that absolutely must run constantly while you're doing your usual UI stuff, then multi-threading (e.g. _beginthreadex() and all the loveliness that accompanies it) is your path.

    I've never tried windows timers calling functions directly via a "TimerProc", since I try to find better ways of doing it first.

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Program Main Loop.

    Why would you want your program to run slowly? The cpu is there to work for you.

    Without more context, your question can't really be answered.

  6. #6
    Join Date
    Sep 2007
    Location
    poland|wrocław
    Posts
    47

    Re: Program Main Loop.

    Quote Originally Posted by wildfire99
    Use Sleep. Sleep(10) to sleep 10 milliseconds, which will suspend your thread for 10ms so the CPU can do other things.

    If you're writing a traditional windows message loop, use PeekMessage() to check for pending messages, then again take a nap with Sleep() when you're done. You can then control the temporal granularity (is that a proper term?) of your processing loops.

    Of course, if you're writing a game program, it's pretty typical for it to eat up 100% of the CPU, as it is a performance-based application and should always have something to do.

    WM_TIMER messages is very easy to use but is not in my experience terribly consistent under 2-3 seconds, especially as you have to get around to processing the message(s) first. If you have something that absolutely must run constantly while you're doing your usual UI stuff, then multi-threading (e.g. _beginthreadex() and all the loveliness that accompanies it) is your path.

    I've never tried windows timers calling functions directly via a "TimerProc", since I try to find better ways of doing it first.

    Instead of PeekMessage() try GetMessage() so you app will be using CPU only when need it, so you can work with out Sleep(n).

  7. #7
    Join Date
    Aug 2001
    Location
    Germany
    Posts
    1,384

    Re: Program Main Loop.

    Sleeping in the main thread is not a good idea (the application will appear to be hung/non-responsive during sleep). I'd suggest to create worker thread, and do the sleeping over there .

    Regards,
    Usman.

  8. #8
    Join Date
    Oct 2007
    Posts
    5

    Re: Program Main Loop.

    Hi felows,

    I'm reading the posts and trying some advices you gave, but i'm sorry to say none of them help me.

    First i tried this Sleep(...) function, witch seems to be a nice way out my problem. It stops the loop, that's true, but it doesn't help with the CPU Usage.

    About using the GetMessage() loop, i'm indeed using a WIN32 api window, but i need at leat 4 turns per second for my app and if i use "while(GetMessage(...))" and all the WinProc stuff, at some point it stops getting back to my code if no action is taken at the window.

    Anyway, i think GCDEF is right in his plea, so i'll give some context to my thread. I've ceated a docked window and i'm trying to display some Roll-On text on it. So i'm using the TextOut(...) function manipulating the offset as the loop turns on. Well, it is a very simple program, so i think it is absurd that it takes all that heavy processing.

    Thank's you guys for wasting time with a newbie like me! I'll keep trying to solve my problem so any help is apreciated.

    JorMaia

  9. #9
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Program Main Loop.

    Well, noname00 already suggested you to use timer (SetTimer and handle WM_TIMER message). Why don't you want to implement it?
    Victor Nijegorodov

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured