Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 28th, 2007, 7:51 AM   #1
Lesliect6
Programmer
 
Join Date: Aug 2005
Posts: 68
Rep Power: 4 Lesliect6 is on a distinguished road
passive form pof waiting

Hello,

I'm adding the finishing touches to my program, and I'd like you to help me with this, I haven't found any way to do it. So, I want the program to execute a series of instructions after every x seconds. Now, I have to find a form of passive waiting during which the program "doesn't do anything", but doesn't use up any or almost no CPU. I've tried it with the clock_t class and various other methods but I always end up with 50 percent of CPU power being used while waiting. I know it's logical, but can someone find a way to do this without using that much CPU?

Thank you,

Leslie

EDIT : sorry for the title
Lesliect6 is offline   Reply With Quote
Old Jun 28th, 2007, 8:14 AM   #2
rwm
Professional Programmer
 
Join Date: Jan 2007
Location: Cape Town
Posts: 291
Rep Power: 2 rwm is on a distinguished road
id also be interested in hearing any solutions to this as well!
rwm is offline   Reply With Quote
Old Jun 28th, 2007, 9:14 AM   #3
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5 grumpy is on a distinguished road
There's no standard way in C/C++ to do this.

However, virtually every modern operating system support means to do this -- although the method is operating system dependent. For example, if you look in the win32 API there is a function named Sleep() that can be used to sleep for (approximately) a specified number of milliseconds and a function named SetTimer() which allows a function to be called periodically.
grumpy is offline   Reply With Quote
Old Jun 28th, 2007, 10:12 AM   #4
Lesliect6
Programmer
 
Join Date: Aug 2005
Posts: 68
Rep Power: 4 Lesliect6 is on a distinguished road
Thank you for the reply. The Sleep() method still uses 50 percent CPU, however I don't know how to use the SetTimer one. Could you give an example? I was also wondering if there is a way of doing this with interrupt handling. It could be much more effective... I recently read about int 8h, does anyone know if it can help us?

Thank you,

Leslie
Lesliect6 is offline   Reply With Quote
Old Jun 28th, 2007, 1:58 PM   #5
Eoin
Hobbyist Programmer
 
Eoin's Avatar
 
Join Date: Jun 2006
Location: Ireland
Posts: 152
Rep Power: 3 Eoin is on a distinguished road
You'll really need to provide more info. SetTimer is a rather simple Win32 API function, but if you're not familiar with the API backbone of Window Procedures and Message then it's not going to be much use on it's own. Is the program Console based or GUI?

As for Sleep, that should, dare I say will, work. Just call it when you want the program to do nothing specifying how long you want it to do nothing for.

If you're in Windows then interrupts are not the way to go.
__________________
Visit my website BinaryNotions.
Eoin is offline   Reply With Quote
Old Jun 28th, 2007, 4:42 PM   #6
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,038
Rep Power: 5 lectricpharaoh will become famous soon enough
One way that works well in console-mode programs (ie, is not dependant on the Windows message pump) is timeSetEvent(). You will need to include the <mmsystem.h> header, as well as link in winmm.lib (or libwinmm.a, if you're using DevC++).

Using this approach, you can set up a callback function to be invoked every x milliseconds, which is essentially what you want to do. Just don't shove too much code into the callback function; remember that since it effectively operates in an interrupt context, you need to keep it short. Don't forget to call timeKillEvent() when you're done, either.

Should you need to do something more complicated, you'll probably want to look into making your program multithreaded, and having a worker thread that periodically does a task, and then sleeps. By using API functions to get the elapsed time it takes for this thread to do its task once, you can calculate how long you'll eed to sleep for. In other words, if you want to do the task once every five seconds, but it takes 2 seconds to do it, you want to sleep for 3, not 5. As a sleeping thread consumes no CPU time (the task scheduler won't give it the CPU), it will use minimal CPU resources.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote
Old Jun 28th, 2007, 5:45 PM   #7
Dameon
Troll
 
Dameon's Avatar
 
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4 Dameon is on a distinguished road
Quote:
Originally Posted by Lesliect6 View Post
The Sleep() method still uses 50 percent CPU
This statement alarms me.
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270
Dameon is offline   Reply With Quote
Old Jun 28th, 2007, 5:47 PM   #8
Lesliect6
Programmer
 
Join Date: Aug 2005
Posts: 68
Rep Power: 4 Lesliect6 is on a distinguished road
To keep it short, it is a managed .NET application. I want it to execute a quite long and complicated code every x seconds. I tried the sleep function, but for some reason the CPU was still working 50 percent.
Lesliect6 is offline   Reply With Quote
Old Jun 28th, 2007, 6:03 PM   #9
Eoin
Hobbyist Programmer
 
Eoin's Avatar
 
Join Date: Jun 2006
Location: Ireland
Posts: 152
Rep Power: 3 Eoin is on a distinguished road
Well if you're going to keep it that short there is very little anyone can do to help. To put it bluntly Sleep() will work, if it isn't then you are doing something wrong. Put the code to be executed inside a loop along with the Sleep instruction set to X seconds.

Unless your app is multi-threaded of course, because Sleep only 'sleeps' one thread so if a second thread is still working away then the cpu is still in use.

P.S. I'm gonna guess you've a dual core so that 50% is really 100% of one core.
__________________
Visit my website BinaryNotions.
Eoin is offline   Reply With Quote
Old Jun 28th, 2007, 6:14 PM   #10
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5 grumpy is on a distinguished road
Quote:
Originally Posted by Lesliect6 View Post
To keep it short, it is a managed .NET application. I want it to execute a quite long and complicated code every x seconds. I tried the sleep function, but for some reason the CPU was still working 50 percent.
Sleep() does not behave like this. It probably means that your "long and complicated" code is using about half the CPU time (making an application sleep between calls of a function does not magically make that function consume no CPU time), or you have another application or thread running that is consuming CPU time [(Sleep() works by yielding your thread's time-slice, and allowing another thread to execute].
grumpy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
FFT DWTIB -> Optimization -> Choosing An Appropriate Run Time Sane Software Design and Algorithms 7 Dec 1st, 2006 10:40 AM
Show or hiding forms/modifying control properties ..from different a form.. cloud- C# 4 Nov 10th, 2006 10:51 AM
Form Submit Blues MegaArcon Python 3 Dec 14th, 2005 4:20 PM
.NET Timer Form closing issue MBirchmeier C# 4 Nov 21st, 2005 10:00 AM
entering data into excel from a form glevine Perl 1 Nov 18th, 2005 5:03 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:34 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC