473,382 Members | 1,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Screensaver application 100% CPU usage

I have written a screensaver application in C# .NET and have noticed
that whenever the animation loop is running - CPU usage always spikes
up close to 100%. My application uses a paint thread which is set at
100ms.

the program looks sort of like this:

// constructor
public Form1()
{
InitializeComponent();

// initialization code

tmrPaintRefresh=new System.Timers.Timer(100);
tmrPaintRefresh.Elapsed+=new
System.Timers.ElapsedEventHandler(tmrPaintRefresh_ Elapsed);

}
private void tmrPaintRefresh_Elapsed(object sender,
System.Timers.ElapsedEventArgs e)
{
// update animation variables

Invalidate();
}

protected override void OnPaint(PaintEventArgs e)
{
// contains only code to draw objects on the screen
}
The program works fine and everything looks as it should but CPU usage
is abnormally high.

my question is: where should I be calling Application.DoEvents();?

how can I restructure it so that it is not so CPU intensive. All it is
doing is animating text on the screen; it should not be such a CPU
hog.

Thanks much,

wushupork
Nov 16 '05 #1
2 2100
wushupork, just to test things out, try putting a thread.Sleep(100) after
Invalidate() and see if that changes your CPU issue.

--
Greg Ewing [MVP]
http://www.citidc.com
"wushupork" <wu*******@gmail.com> wrote in message
news:ff**************************@posting.google.c om...
I have written a screensaver application in C# .NET and have noticed
that whenever the animation loop is running - CPU usage always spikes
up close to 100%. My application uses a paint thread which is set at
100ms.

the program looks sort of like this:

// constructor
public Form1()
{
InitializeComponent();

// initialization code

tmrPaintRefresh=new System.Timers.Timer(100);
tmrPaintRefresh.Elapsed+=new
System.Timers.ElapsedEventHandler(tmrPaintRefresh_ Elapsed);

}
private void tmrPaintRefresh_Elapsed(object sender,
System.Timers.ElapsedEventArgs e)
{
// update animation variables

Invalidate();
}

protected override void OnPaint(PaintEventArgs e)
{
// contains only code to draw objects on the screen
}
The program works fine and everything looks as it should but CPU usage
is abnormally high.

my question is: where should I be calling Application.DoEvents();?

how can I restructure it so that it is not so CPU intensive. All it is
doing is animating text on the screen; it should not be such a CPU
hog.

Thanks much,

wushupork

Nov 16 '05 #2
Thanks Greg,

That seemed to have solved my problem. On a related note, I have a
system tray app that communicates with the screensaver. All it does is
listens for a message on a port from the screensaver using a
TcpListener.

How do I write a simple socket app so that it does not do a busy wait.

Right now my app looks like this

// event handler for form load
private void Form1_Load(object sender, System.EventArgs e)
{
thread1 = new Thread(new ThreadStart(Listen));
thread1.IsBackground = true;
}

private void Listen()
{
Socket socketForClient;
//TcpListener is listening on the given port...
TcpListener tcpListener = new TcpListener(SCREENSAVER_PORT);

try
{
tcpListener.Start();

while (blnListening)
{

if (tcpListener.Pending())
{
//Accepts a new connection...
socketForClient = tcpListener.AcceptSocket();

if (socketForClient.Connected)
{
// do my stuff
}

socketForClient.Close();
}

// newly added sleep to free up some CPU
Thread.Sleep(100);

}
}
catch(Exception e)
{
Console.WriteLine(e.ToString()) ;
}
finally
{
tcpListener.Stop();
}

// end of code

since adding the Thread.Sleep(100) here, the CPU isn't so busy, but I
am getting the feeling that it is still doing a busy wait. What should
I be doing?

thanks,

wushupork
Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: The Roys | last post by:
Hi Im doing something wrong in quitting the Word.Application in my VB program. I have General Declarations Dim AppWord As Word.Application Form_Load() Set AppWord =...
1
by: klappnase | last post by:
Hello everyone, does anyone know a way to stop the screensaver under linux from starting up while a certain process is running? I need this because I experience a memory leak while recording...
0
by: Luke | last post by:
I am trying to capture an event when the screensaver starts. I have code working on Windows XP etc when the Password/Welcome Screen is enabled . Using the Fast User Switching Notifications. ...
0
by: Rick | last post by:
I am trying to stop the screen saver from running based on a external trigger from our application. I have a .Net application and I am using interop to make a SystemParameterInfo() call to...
4
by: Paul Smith | last post by:
I have been given an unusual task to create a Windows Service to do something periodically to make the screensaver timer reset. We have a general policy of locking computers automatically after 10...
4
by: Simon Mercer | last post by:
I have been trying to build a screensaver that will connect to a database and display specific data depending on the user that is logged in. I have managed to get a simple data access screensaver...
3
by: Ron Nanko | last post by:
Hi, I'm trying to setup a screensaver via a C# application. What I currently do is this: unsafe public static extern short SystemParametersInfo (int uiAction, int uiParam, int* pvParam, int...
0
by: jim.long | last post by:
I am in the learning fazes of writing a c# screensaver that will take into account a second monitor. the basic code I have has a problem and i am not sure what it might be. What it is doiing is...
6
by: mateusz.zajakala | last post by:
Hello, I have strange problem. I let my application work (it tests in loop with 3sec brakes if it's possible to connect to remote pc /by ssh/) and do nothing until screensaver activates. After...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.