473,657 Members | 2,266 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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()
{
InitializeCompo nent();

// initialization code

tmrPaintRefresh =new System.Timers.T imer(100);
tmrPaintRefresh .Elapsed+=new
System.Timers.E lapsedEventHand ler(tmrPaintRef resh_Elapsed);

}
private void tmrPaintRefresh _Elapsed(object sender,
System.Timers.E lapsedEventArgs e)
{
// update animation variables

Invalidate();
}

protected override void OnPaint(PaintEv entArgs 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.DoE vents();?

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 2115
wushupork, just to test things out, try putting a thread.Sleep(10 0) after
Invalidate() and see if that changes your CPU issue.

--
Greg Ewing [MVP]
http://www.citidc.com
"wushupork" <wu*******@gmai l.com> wrote in message
news:ff******** *************** ***@posting.goo gle.com...
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()
{
InitializeCompo nent();

// initialization code

tmrPaintRefresh =new System.Timers.T imer(100);
tmrPaintRefresh .Elapsed+=new
System.Timers.E lapsedEventHand ler(tmrPaintRef resh_Elapsed);

}
private void tmrPaintRefresh _Elapsed(object sender,
System.Timers.E lapsedEventArgs e)
{
// update animation variables

Invalidate();
}

protected override void OnPaint(PaintEv entArgs 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.DoE vents();?

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(obje ct sender, System.EventArg s e)
{
thread1 = new Thread(new ThreadStart(Lis ten));
thread1.IsBackg round = true;
}

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

try
{
tcpListener.Sta rt();

while (blnListening)
{

if (tcpListener.Pe nding())
{
//Accepts a new connection...
socketForClient = tcpListener.Acc eptSocket();

if (socketForClien t.Connected)
{
// do my stuff
}

socketForClient .Close();
}

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

}
}
catch(Exception e)
{
Console.WriteLi ne(e.ToString() ) ;
}
finally
{
tcpListener.Sto p();
}

// end of code

since adding the Thread.Sleep(10 0) 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
13565
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 = CreateObject("Word.Application")
1
6646
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 wav-files every time the screensaver starts. It is a Tkinter application, btw. Any help would be very appreciated. Thanks
0
4175
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. However I am unable to detect the screensaver starting when the screensaver password option is disabled. When using the WndProc Overrides I am unable to capture the screensaver event unless the application is active . Thank you in advance for any...
0
528
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 determine if the screensaver is running and that works OK, but I do not see a way to force it to stop running. I tried to deactivate and reactivate it but that does not force it to quit. I do not want to disable it all together. I really just want a...
4
5095
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 minutes of inactivity. This windows service would be used on some machines to prevent the lock from occurring every 10 minutes. I have tried to reposition the mouse pointer with the Cursor class in a service, but it does not work -- always...
4
5497
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 to work, however, i can't get the application to work with some prebuilt assemblies i have. Also, when the screensaver is running under the windows screensaver settings it also will not access the config file. Any pointers would be greatly...
3
5649
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 fWinIni); .... try
0
1706
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 executing on the primary screen and on that instance Close(); launches on the second screen. the code is: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ using System; using System.Drawing; using...
6
2129
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 it activates I move mouse to get back to Windows. And now: 1. When I have screensaver protected with a password, whole application hangs. It even blocks taskbar. 2. When I have screensaver without password, everything is just fine. That's not the...
0
8407
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8837
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8739
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8612
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7347
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6175
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5638
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4171
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1732
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.