cxWidgets 1.0
cxTimer.h
Go to the documentation of this file.
1// Copyright (c) 2026 Eric N. Oulashin
2#ifndef __CXTIMER_H__
3#define __CXTIMER_H__
4
5// Copyright (c) 2005-2007 Michael H. Kinney
6
7#include "cxFunction.h"
8#include <string>
9#include <memory>
10#include <mutex>
11#include <atomic>
12
13namespace cx {
14
21{
22 public:
30 cxTimer(const std::shared_ptr<cxFunction>& pFuncPtr, unsigned int pDelayMS, bool pWaitInSeparateThread = true);
31
32 virtual ~cxTimer();
33
39 virtual void function(const std::shared_ptr<cxFunction>& pFuncPtr);
40
46 virtual std::shared_ptr<cxFunction> function() const;
47
53 virtual void delay(unsigned int pDelay);
54
60 virtual unsigned int delay() const;
61
67 virtual void waitInSeparateThread(bool pWaitInSeparateThread);
68
74 virtual bool waitInSeparateThread() const;
75
79 virtual void start();
80
86 virtual void stop(bool pRunFunction = true);
87
91 virtual bool isWaiting() const;
92
100 virtual std::string cxTypeStr() const;
101
102 private:
103 std::shared_ptr<cxFunction> mFunction; // The function to run after the delay
104 unsigned int mDelay; // The delay in MS
105 bool mWaitInSeparateThread; // Whether or not to wait in a separate thread
106 //int mPID = -1; // The process ID of wait()
107 std::atomic<bool> mIsWaiting{false}; // Status indicator for whether the timer is currently waiting for time to elapse
108 std::atomic<bool> mRunFunctionAfterWaiting{true}; // Control for whether to run the function after the time elapses
109 std::mutex mWaitMutex; // Mutex to be used in wait(), to lock anything used in there
110
111 // Waits a number of milliseconds equal to mDelay
112 // and then runs the function.
113 void wait();
114};
115
116} // namespace cx
117
118#endif
Represents a timer that will run a function after.
Definition cxTimer.h:21
virtual unsigned int delay() const
Returns the delay (in MS) that is set in the object.
Definition cxTimer.cpp:46
virtual std::shared_ptr< cxFunction > function() const
Returns the function pointer that is set in the object.
Definition cxTimer.cpp:35
virtual ~cxTimer()
Definition cxTimer.cpp:25
virtual void start()
Starts the timer.
Definition cxTimer.cpp:62
virtual bool isWaiting() const
Returns whether the timer object is currently waiting for time to elapse.
Definition cxTimer.cpp:76
virtual std::string cxTypeStr() const
Returns the name of the cxWidgets class. This can be used to.
Definition cxTimer.cpp:81
virtual void function(const std::shared_ptr< cxFunction > &pFuncPtr)
Sets/changes the function pointer.
virtual bool waitInSeparateThread() const
Returns the setting for whether or not to wait in a separate thread.
Definition cxTimer.cpp:57
virtual void stop(bool pRunFunction=true)
Stops the timer.
Definition cxTimer.cpp:70
cxBorderChars.h - Defines border characters to be used in drawing a box (i.e., in cxWindow and all it...
Definition cxApp.cpp:5