00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "JThread.h"
00028
00029 #include "JTime.h"
00030
00031 #if !defined(AFX_TIMER_H__23E586ED_E9EF_4089_9235_B98DDD7C1BA6__INCLUDED_)
00032 #define AFX_TIMER_H__23E586ED_E9EF_4089_9235_B98DDD7C1BA6__INCLUDED_
00033
00034 namespace cmlabs {
00035
00036 class TimerReceiver
00037 {
00038 public:
00039 virtual bool receiveTimerString(JString str) = 0;
00040 };
00041 class TimerObjectReceiver
00042 {
00043 public:
00044 virtual bool receiveTimerObject(Object* obj) = 0;
00045 };
00046
00047
00048 class Timer : public JThread, public Object
00049 {
00050 public:
00051 Timer(JString strData, long ms);
00052 Timer(Object* obj, long ms);
00053 Timer(JString strData, long ms, void (*function)(JString string));
00054 Timer(JString strData, long ms, TimerReceiver* receiver);
00055 Timer(Object* obj, long ms, void (*function)(Object* object));
00056 Timer(Object* obj, long ms, TimerObjectReceiver* receiver);
00057 virtual ~Timer();
00058
00059 Object* clone() const {return NULL;}
00060 void run();
00061 void cancel();
00062 long msLeft();
00063 long waitFor();
00064 bool hasEnded();
00065 long msSinceEnded();
00066
00067 Object* getAndRemoveObject();
00068
00069
00070 protected:
00071
00072 JString data;
00073 Object* object;
00074 JTime startedAt;
00075 JTime endedAt;
00076
00077 void (*stringCallback)(JString strData);
00078 void (*objectCallback)(Object* obj);
00079 TimerReceiver* timerReceiver;
00080 TimerObjectReceiver* timerObjectReceiver;
00081
00082 JSemaphore extSem;
00083 JSemaphore timerSem;
00084 bool shouldContinue;
00085 long msRuntime;
00086
00087 void OnTimer();
00088 };
00089
00090 }
00091
00092
00093
00094 #endif // !defined(AFX_TIMER_H__23E586ED_E9EF_4089_9235_B98DDD7C1BA6__INCLUDED_)