Jump to content
  • Advertisement
Sign in to follow this  
Black Knight

[.net] How to stop an application when its minimized?

This topic is 4126 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

My application always uses the whole cpu or 50% on a dual core processor. I want to make it use 0% cpu when its minimized. I did this in c++ win32 by checking the AppActivate windows message.And stopping the render loop if the application is inactive. But in c# I couldnt do it. Here is my loop and main:
[STAThread]
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

			System.Windows.Forms.Application.Idle += new EventHandler(Application_Idle);
			
            using(m_MainWindow = new MainWindow())
            {
                //m_MainWindow.Show();
				Application.Run(m_MainWindow);
              /*  while(m_MainWindow.Created){
                    //m_MainWindow.MainLoop();
                
                    Application.DoEvents();
                }*/
            }
        }

static void Application_Idle(object sender, EventArgs e)
		{
			while (AppStillIdle)
			{
				 m_MainWindow.MainLoop();
				
			}

		}
		static private bool AppStillIdle
		{
			get
			{
				NativeMethods.Message msg;
				return !NativeMethods.PeekMessage(out msg, IntPtr.Zero, 0, 0, 0);
			}
		}

Its from tom millers blog.It runs fine but always uses the CPU.

Share this post


Link to post
Share on other sites
Advertisement
Uh nevermind my noobness.I just solved it.
I added a bool to my main window.

private bool m_AppActive = true;

Then I added to event handlers for the main window.

private void MainWindow_Deactivate(object sender, EventArgs e)
{
AppActive = false;
}

private void MainWindow_Activated(object sender, EventArgs e)
{
AppActive = true;
}



Then in the idle event :


static void Application_Idle(object sender, EventArgs e)
{
while (m_MainWindow.AppActive == true && AppStillIdle)
{
m_MainWindow.MainLoop();
}


}



Now when the application is minimized or it is deactivated by switching to another window it doesnt use any cpu.
The only problem is when I return to my application the hirestimer gets the elapsed time till the last frame and because its a huge amount everything happens very fast for a moment.
I guess I need to stop the timer when the app gets deactivated and restart it when activated.

Share this post


Link to post
Share on other sites
Another way to do it is in your Application_Idle() loop you can just test for the form being minimized and return if it is.

if (this.WindowState == FormWindowState.Minimized)
return;


You could also add a bit of a sleep in there to use up less CPU

if (this.WindowState == FormWindowState.Minimized)
{
System.Threading.Thread.Sleep(100);
return;
}


You can also do the same thing if the form has lost focus

if (!this.Focused)
return;


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!