473,387 Members | 1,550 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,387 software developers and data experts.

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 3290

"John" <no****@nospam.com> schrieb im Newsbeitrag
news:CI****************@newsfe1-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<TIMERS> 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
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...
2
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. ...
2
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 :...
9
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;...
6
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...
12
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
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...
4
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...
4
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...
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: 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...
0
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...

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.