User
User

Reputation: 66051

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.

Upvotes: 3

Views: 2666

Answers (2)

zar
zar

Reputation: 12247

You should use a secondary thread and tell it wait 10 seconds and than call the function. This way it will have the least impact on rest of the your program in terms of GUI responsiveness and ensure your function will be called irrespectively after 10 seconds.

Upvotes: 0

sonofdelphi
sonofdelphi

Reputation: 2036

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.

Upvotes: 3

Related Questions