Sign in to follow this  

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

This topic is 3840 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
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

This topic is 3840 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this