tags:

views:

179

answers:

3

Someone suggested to me to use Call Back Functions to implement a timer to run in the background while my Server application reads input from clients. I tried looking at explanations online, but was hoping if someone could give me a simpler analogy.

Thanks.

A: 

Your question is fairly unclear, but it's probable they were suggesting you create a thread and run your function in that thread.

This can be done by subclassing a system-specific Thread class; by constructing the same class with some sort of call-back function as an argument; by creating a timer that invokes a callback function after some time limit... without a more specific question I can't give more specific advice.

Conrad Meyer
Is it possible to do this without a thread?Are threads easy to create?I haven't learn't multithreading yet, thats why I usually stay away from the answers talking about threads. But it seems it may be the only way for me...
NeverAgain
Any time you want to "run something in the background", you are probably thinking about using a separate thread or process.
Conrad Meyer
+1  A: 

Analogy?

Take a look here for a brief explanation of callback functions:
What is a “callback” in C and how are they implemented?

Using a timer with a callback would be saying 'call function x every y seconds' and with a system that supports multitasking, that function would be called every y seconds in a second thread of execution, no matter what the original function might be doing.

Edit: As has been suggested in another answer, the system might not create a second thread for you, in which case you'd have to create the thread yourself and set up the callback from that thread.

Edit: In Windows, you can use the SetTimer function. It will post a WM_TIMER message to your window's message queue, which your message loop might handle itself or hand over to the default message procedure to call a callback function you've specified. I'm not sure what happens if you don't have a window, but give it a try.

aib
+2  A: 

There are two separate ways to implement a timer using callbacks in Windows, SetTimer and timeSetEvent. The basics are:

  1. SetTimer uses messages, even if you use a callback (the callback function is invoked as a result of processing a message). So SetTimer isn't viable if you don't run a message pump.

  2. Callbacks are called by the operating system, which doesn't know a C++ "this" pointer from a hole in the ground, so your callback either has to be a global C-style function or a static member.

  3. timeSetEvent is part of the "multimedia" timer family, and doesn't require a message pump. The observations about the callback function signature above still apply though. The lack of requirement for a message pump can be important if you're writing a console app though.

  4. You might also consider threading and CreateWaitableTimer, but I don't use waitable timers very often so can't comment on them.

If you need to do work in the background, then threading can be a much more elegant way to address the problem. You don't have to divide the work up into chunks when you're threading (which you do if you're kicking the work from a timer). But of course your thread can't touch the GUI, so life can get a little complicated when you start threading. There's an intro to worker threads on my website here.

Bob Moore