C ++: calling a schedule function in the future?

Using Visual C ++ with MFC. When a certain event occurs in my code, I want the function to be called after 10 seconds to perform some operation. The event handling happens in a static library that has no direct references to MFC (and I would like to keep it that way).

How do I assign a function to be called at some point in the future? Am I using a timer? How do I disable the timer (which is MFC specific) so my business code doesn't directly depend on the GUI? Or maybe something else besides a timer?

Update

Started reading about Command Pattern recently which seems promising for my situation. Description (my attention):

In object-oriented programming, a command pattern is a project pattern in which an object is used to represent and encapsulate all the information required to call a method at a later time.

+3


source to share


2 answers


Approach 1 Write a wrapper function to call the static library. Hibernate for the required duration before calling the static library.

Approach 2 Use a Win32 timer http://www.codeproject.com/Articles/1236/Timers-Tutorial#Win32Timers



You can avoid the MFC dependency in both approaches.

+3


source


You have to use an extra thread and tell it to wait 10 seconds and then call the function. This way, it will have the least impact on the rest of your program in terms of GUI responsiveness, and ensures that your function is called independently after 10 seconds.



0


source







All Articles