473,569 Members | 2,772 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Timer in C (like in Java)

Hello, I need a facility in C, like a set of functions, to manage a
timer: I should be able to initialize it with a certain period of time
(like x msec), start it, and every x msec it should execute a certain
function. In a few words, I need something like the java.util.Timer
class, but for C (of course not a class).
Do you know of any implementation of something like that (if possible
real-time)?
Thanks a lot
Jun 27 '08 #1
14 3015
Alexander Mahone <sa************ ******@gmail.co mwrites:
Hello, I need a facility in C, like a set of functions, to manage a
timer: I should be able to initialize it with a certain period of time
(like x msec), start it, and every x msec it should execute a certain
function. In a few words, I need something like the java.util.Timer
class, but for C (of course not a class).
Do you know of any implementation of something like that (if possible
real-time)?
Not in standard C. Try a newsgroup that deals with your operating
system.

--
Keith Thompson (The_Other_Keit h) <ks***@mib.or g>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #2
Alexander Mahone wrote:
Hello, I need a facility in C, like a set of functions, to manage a
timer: I should be able to initialize it with a certain period of time
(like x msec), start it, and every x msec it should execute a certain
function. In a few words, I need something like the java.util.Timer
class, but for C (of course not a class).
Do you know of any implementation of something like that (if possible
real-time)?
Thanks a lot
Standard C has no facilities for what you want to do. However POSIX
specifies ualarm() and setitimer() for interrupting a process after a
specified period. A signal handler can then call your function.

<http://www.mkssoftware .com/docs/man3/ualarm.3.asp>
<http://www.mkssoftware .com/docs/man3/setitimer.3.asp >

Jun 27 '08 #3
In article <fu*********@re gistered.motzar ella.org>,
santosh <sa*********@gm ail.comwrote:
>Standard C has no facilities for what you want to do. However POSIX
specifies ualarm() and setitimer() for interrupting a process after a
specified period. A signal handler can then call your function.
Neither function was part of POSIX.1-1990.

ualarm() was added to POSIX as of Issue 4, Version 2, and moved from
X/OPEN UNIX extension to BASE of of issue 5 (which I think was 2002.)
As of issue 6 (2004), ualarm() is marked obsolescent.
http://www.opengroup.org/onlinepubs/...ns/ualarm.html

setitimer() was also added and moved at the same issues as for ualarm(),
but setitimer() is not marked obsolescent.
http://www.opengroup.org/onlinepubs/...getitimer.html

For information about what the -recommended- POSIX timer functions are
and their various trade-offs, a unix programming newsgroup should
be consulted.
--
"Walter exemplified class." -- Paul Tagliabue
Jun 27 '08 #4
Walter Roberson wrote:
In article <fu*********@re gistered.motzar ella.org>,
santosh <sa*********@gm ail.comwrote:
>>Standard C has no facilities for what you want to do. However POSIX
specifies ualarm() and setitimer() for interrupting a process after a
specified period. A signal handler can then call your function.

Neither function was part of POSIX.1-1990.

ualarm() was added to POSIX as of Issue 4, Version 2, and moved from
X/OPEN UNIX extension to BASE of of issue 5 (which I think was 2002.)
As of issue 6 (2004), ualarm() is marked obsolescent.
http://www.opengroup.org/onlinepubs/...ns/ualarm.html

setitimer() was also added and moved at the same issues as for
ualarm(), but setitimer() is not marked obsolescent.
http://www.opengroup.org/onlinepubs/...getitimer.html

For information about what the -recommended- POSIX timer functions are
and their various trade-offs, a unix programming newsgroup should
be consulted.
You're right. Apologies to the OP for mentioning an obsolescent
function.

Jun 27 '08 #5
On Wed, 23 Apr 2008 21:58:05 +0530, santosh wrote:
Standard C has no facilities for what you want to do. However POSIX
specifies ualarm() and setitimer() for interrupting a process after a
specified period. A signal handler can then call your function.
Only if you're really careful. Only some functions are safe to call in a
signal handler, or functions called from a signal handler. your system's
'man 7 signal()' should give you a list of safe functions to call from a
signal handler.

A unix newsgroup should give more information.
Jun 27 '08 #6
jon
there is a simple timer, but it works on the same process (which means
that the program will stop till the timer is done):

sleep(int amount_of_milli seconds);

in windows programing there are ways to create a separate process to
control the timer:

here is a link to a msdn site:

http://msdn2.microsoft.com/en-us/library/ms632592.aspx

(if it doesn't work, try searching (on the same page) for timers and
windows)

// Set two timers.

SetTimer(hwnd, // handle to main window
IDT_TIMER1, // timer identifier
10000, // 10-second interval
(TIMERPROC) NULL); // no timer callback

SetTimer(hwnd, // handle to main window
IDT_TIMER2, // timer identifier
300000, // five-minute interval
(TIMERPROC) NULL); // no timer callback
Jun 27 '08 #7
In article <3b************ *************** *******@2g2000h sn.googlegroups .com>,
jon <jm******@hotma il.comwrote:
>there is a simple timer, but it works on the same process (which means
that the program will stop till the timer is done):
>sleep(int amount_of_milli seconds);
sleep() is not part of standard C. It is a common operating system
extension, but the original poster did not specify an OS.
--
"Allegories are in the realm of thoughts, what ruins are in
the realm of things." -- Walter Benjamin
Jun 27 '08 #8
ro******@ibd.nr c-cnrc.gc.ca (Walter Roberson) writes:
In article <3b************ *************** *******@2g2000h sn.googlegroups .com>,
jon <jm******@hotma il.comwrote:
>>there is a simple timer, but it works on the same process (which means
that the program will stop till the timer is done):
>>sleep(int amount_of_milli seconds);

sleep() is not part of standard C. It is a common operating system
extension, but the original poster did not specify an OS.
Furthermore, it's defined differently on different systems. On POSIX
systems, for example, the argument is a number of seconds.

--
Keith Thompson (The_Other_Keit h) <ks***@mib.or g>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #9
On 23 Apr 2008 at 22:34, Keith Thompson wrote:
ro******@ibd.nr c-cnrc.gc.ca (Walter Roberson) writes:
>sleep() is not part of standard C. It is a common operating system
extension, but the original poster did not specify an OS.

Furthermore, it's defined differently on different systems. On POSIX
systems, for example, the argument is a number of seconds.
You're right, but there are also usleep and nanosleep, for shorter
intervals.

Jun 27 '08 #10

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

Similar topics

1
11096
by: Geoff | last post by:
Hey.. I'm having problems with stopping this timer from outside the 'runTimer()' class.. i have no problems calling 'cancel()' from inside the run() method but I want to be able to stop the Timer when I press a button (stopTimer()). With the code I've got here, i get an error saying 'void cannot be dereferenced' ... has anyone got any ideas...
3
319
by: Mr. B | last post by:
My current app has a timer that I kick ON in my Form1_Load as follows: ' Set Up the Timer Function Dim t As New System.Timers.Timer(12000) ' 1000 = 1 Second t.Enabled = True ' False to Turn OFF AddHandler t.Elapsed, AddressOf TimerFired This in turns fires up my sub called "TimerFired" every 2 minutes... In this sub, I check a file for...
0
1159
by: Daniel Maycock via .NET 247 | last post by:
I can't get my threading timer to show a splash screen panel for six seconds, then move onto the next panel. The timer just doesn't tick (this is aparent when I set the time to wait to 100 and it never fires off the sub) I've tried using java, I've tried using the system timer - this is my last hope - please help! Here's my Code...
8
2160
by: bearophileHUGS | last post by:
Hello, I have four things to ask or to suggest, sorry if they seem basic or already discussed. ------------------- I am still ignorant about Tkinter. This little program, after pressing the "Go" eats more and more RAM, is it normal? Can it be avoided? (In normal programs this is isn't a real problem). ! import Tkinter
3
1764
by: Daniel Maycock via .NET 247 | last post by:
I can't get my threading timer to show a splash screen panel for six seconds, then move onto the next panel. The timer just doesn't tick (this is aparent when I set the time to wait to 100 and it never fires off the sub) I've tried using java, I've tried using the system timer - this is my last hope - please help! Here's my Code...
1
1958
by: enzoJava | last post by:
Hii, I have this program and i need help on printing out the total time, can anyone help me on this? If there is a better way to do this let me know Thnx import javax.swing.*; import javax.swing.Timer; import java.awt.event.*;
4
5426
by: puntino | last post by:
Hi I have created my Alarm, at compile time I don't have any problem, but at run time I receive the follo wing messages: Exception in thread "main" java.lang.NullPointerException at java.util.Timer.sched(Unknown Source) at java.util.Timer.scheduleAtFixedRate(Unknown Source) at Alarm.AlarmExecute(Alarm.java:20) at Test.main(Test.java:11)...
8
2338
by: sip.address | last post by:
Hello, I'm trying to find some existing (and simple if possible) timer queue implementation. Does anybody know a simple skeleton to use as example? I just need to send simple (relative) timeouts. Thought about some possibilities but would prefer to use something already tested. Thanksç
0
7609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8118
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...
1
7666
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6278
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...
0
5217
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...
0
3651
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...
0
3636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2107
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1208
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.