Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++: Schedule function call in the future?

Using Visual C++ with MFC. When a certain event occurs in my code, I want to set a function to be called 10 seconds later to perform some activity. The handling of the event happens in a static library that doesn't have any direct links to MFC (and I'd like to keep it that way).

How can I schedule a function to be called at some point in the future? Use a Timer I guess? How do I decouple the Timer (which is an MFC dependency) so my business code doesn't have a direct dependency on the GUI? Or maybe something else besides a timer?

Update

Recently started reading about the Command Pattern which seems promising for my situation. The description is (my emphasis):

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

like image 670
User Avatar asked Jun 12 '26 20:06

User


1 Answers

Approach 1 Write a wrapper function for the call to the static library. Before the static-library call, do a sleep for the required duration.

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

You can avoid the MFC dependency in both approaches.

like image 65
sonofdelphi Avatar answered Jun 16 '26 11:06

sonofdelphi