473,748 Members | 2,551 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Timer or Timeout Class for Linux AND Windows ?


I'm trying to write a program that's run on both Windows 98 and Linux.

I need a timer, that I can start and when it hits a limit, it needs to
call a callback function. I also need to be able to abort the timer
before it hits the limit - it's the classic timeout type thing.

I'll readily admit I'm no expert on C++, but after a trawl of the WWW
pages, there appears to be two solutions depending on whether it's Linux
or Windows.

Surely the purpose of C++ and OOP is that we don't all keep on
reinventing the wheel, and that a Class should compile and be usable on
whichever environment we wish.

Is there a freely available Class I can download from somewhere that
will work on Linux and Windows ?

I just need to start a timer that has a callback facility, and then
stop the timer, or restart the timer.

Must exist surely.

Jul 22 '05 #1
1 3309

"John" <no****@nospam. com> schrieb im Newsbeitrag
news:CI******** ********@newsfe 1-win.ntli.net...

I'm trying to write a program that's run on both Windows 98 and
Linux.

I need a timer, that I can start and when it hits a limit, it needs
to call a callback function. I also need to be able to abort the
timer before it hits the limit - it's the classic timeout type
thing.

I'll readily admit I'm no expert on C++, but after a trawl of the
WWW pages, there appears to be two solutions depending on whether
it's Linux or Windows.

Surely the purpose of C++ and OOP is that we don't all keep on
reinventing the wheel, and that a Class should compile and be usable
on whichever environment we wish.

Is there a freely available Class I can download from somewhere that
will work on Linux and Windows ?

I just need to start a timer that has a callback facility, and then
stop the timer, or restart the timer.

Must exist surely.


There's some x-platform thread classes. Start a thread that uses time
from time.h (x-platform) to wait for a single time to be elappsed and
then call a given function.

From the scrathc of my head it might look like:

struct TIMERS
{
long nSecs;
long last;
void (*pFn)(void);
};
std::vector<TIM ERS> g_timers;

void MyTimerThread
{
time_t start = time(NULL);
for(;;)
{
time_t t = time(NULL);
for(long i=0; i<g_timers.size (); ++i)
{
if (g_timers[i].last+g_timers[i].nSecs > t)
{
g_timers[i].last=t;
g_timers[i].pFn();
}
}
Sleep_some_time (); // Don't take 100% CPU power
};
}



Jul 22 '05 #2

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

Similar topics

3
2518
by: ray | last post by:
Hi, I just wrote a windows service application to call a function of another object periodically. I used System.Server.Timer and I found that it works fine within the first 2 hours but the onTimer event wouldn't be raised again after few hours. I checked the msdn and Microsoft claims that it is a bug in their .net framework as follows: http://support.microsoft.com/default.aspx?scid=kb;en-us;842793
2
15695
by: andrewcw | last post by:
I am trying to do a windows service with C#. I am using as a base the VB.NET article in VS, but I thing the WITHEVENTS timer notation is a delegate. Can anyone provide sample code & anh hints. Thanks Andrew
2
8464
by: hnkien | last post by:
Hi, I am writing a windows service with threading.timer for 10 seconds but it didn't work. Here are my code: namespace SchedulerService { public class ScheduleService : System.ServiceProcess.ServiceBase { private System.ComponentModel.IContainer components; protected System.Threading.Timer tmrThreadingTimer;
9
7285
by: HL | last post by:
I am using VS 2005 Beta - C# Problem: The Timer fires a few milliseconds before the actual Due-Time Let's say a timer is created in the following manner: System.Threading.Timer m_timer = null; Let's declare a constant Int32 m_TimePeriod = 10000;
6
10243
by: paresh | last post by:
Hi all, I need to set timer in C/linux like alram, such that i will get a timeout signal after specific timeout and my process remain executing as is it. I can use signal(SIGALRM, xyz) and then alarm(some value in sec), there is a constraint in this as i can pass timeout only in seconds and i need in milli sec. Any idea how todo this.
12
2123
by: John | last post by:
Hi I have a timer on my form. The problem is no matter how large an interval I set, the timer_tick event always gets called very fast. What am I doing wrong? Thanks Regards
2
3953
by: Dipankar | last post by:
In an Windows application I have created a Timer to monitor some WorkerThread's state. Once the worker thread stops the timer should also dispose after displaying the message that Worker Thread is done. // Instance variables private Thread workerThread = null; private System.Threading.Timer workerThreadMonitorTimer = null; /// Code inside a button click event ThreadStart workerThreadStart = new ThreadStart(DoWork);
4
5120
by: gaddamreddy | last post by:
Hai to all frns, Reqirement 1: 1.I need to send a Request to one server through UDP socket.at that time i have to start a timer (setted to 2 sec) if i wont get the Response/ACK from that corresponding ACK from that server within 2 sec.i need to send the Request again. for this i did like this. #include <sys/types.h> #include <sys/socket.h>
4
14404
by: Lauren Quantrell | last post by:
I have just put together a vb.net app and now need to provide it to users. This application needs to run the code in a sub every 60 seconds from a Windows Service application. I have the functionality of the sub working fine but I cannot figure out how to run the timer. The sub DoSomethingHere() needs to run every 60 seconds. It doesn't need to change the interval or anything like that. It just needs to run every 60 seconds as long as...
0
8831
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9555
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
9376
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
9250
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
8247
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...
0
6076
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
4607
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
2787
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
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.