[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);
}
}
[.net] How to stop an application when its minimized?
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:
Its from tom millers blog.It runs fine but always uses the CPU.
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.
Then in the idle event :
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.
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.
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.
You could also add a bit of a sleep in there to use up less CPU
You can also do the same thing if the form has lost focus
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;
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement