Skip to main content
GameDev.net gamedev.net
🔒 Locked

[.net] Pausing processing with .Net (C#)

Started by reaptide Jun 30, 2004 at 11:16 PM 5 replies 3.9k views
Original Post
reaptide
reaptide
Hey there, I'm porting an old C++ game to C# at the moment and I'm having an issue with the message loop. Here is my code:


[DllImport("User32.dll")]
private static extern void WaitMessage();

static void Main(string[] CommandLine)
{
    // Create a rendering window
    RenderWindow Window = new RenderWindow();

    // Enter the message loop and keep going as long as the window is open
    while(!Window.IsClosed)
    {
        // Process messages
        Application.DoEvents();

        // If the window is in focus do something useful, otherwise go to sleep for a bit
        if (Window.IsActive)
        {
            // Do Something Useful
        }
        else
            WaitMessage();
    }
}
As you can see I'm using the interop services to import WaitMessage(). I'm using that to suspend processing while the window is not in focus. Processing resumes once the window gets a GotFocus event (Or any other event for that matter). I'm betting that there is a better way to do this than importing a Win32 function. I was using Thread.Sleep() before and that worked but still left the program using up some CPU time. Are there any other alternatives?
Zipster
Zipster
The application still needs the ability to handle events even if it isn't in focus, so you must call Application.DoEvents to handle events regardless. I wouldn't even call WaitMessage at all. Interop can be expensive too.
reaptide
reaptide
Yeah that makes sense. I want the program to be eating every CPU cycle it can get while in focus but stop using as much as possible when not in focus (For example, I task out of the program to go do something else for a while). As I have it there if I remove the WaitMessage() bit the program will continually loop, calling DoEvents() each time even though the window is not in focus and not doing anything. This will keep hogging the CPU.

What WaitMessage supposedly does is halt program processing until the program gets a message. This worked kind of nice in C++ since I didn't need to worry about having my program check for messages every now and then, it would just "wake up" when one was sent to it.

Perhaps another way, which would avoid using interop, would be to have the program goto sleep for a while when out of focus and check back to see if there are any messages. Replace WaitMessage() with Thread.Sleep(500) for example.
reaptide
reaptide
Woah! Never even noticed this new forum. Thanks for the move, whoever did it.
Holy Fuzz
Holy Fuzz
while(/*your window is not in focus*/)
{
System.Threading.Thread.Sleep(100);
System.Windows.Forms.Application.DoEvents();
}

That's how I handle it anyway. The CPU usage is basically 0, though technically not quite.
CWIZO
CWIZO
Yes, better use System.Threading.Thread.Sleep ().

In gneral dont p/invoke if the .NET has the thing you need.
-----------------SloGameDev.net | My homepageQ:What does a derived class in C# tell to it's parent?A:All your base are belong to us!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.