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
00028
00029
00030
00031
00032
00033 #include "CommonFramework.h"
00034
00035
00036 enum
00037 {
00038 EPanicAlreadyActive=1000,
00039 };
00040
00041
00043
00044
00045
00047 class CTimedMessenger : public CTimer
00048 {
00049 public:
00050
00051 CTimedMessenger();
00052
00053 ~CTimedMessenger();
00054
00055 public:
00056
00057 static CTimedMessenger* NewLC(const TDesC& aGreeting,
00058 TInt aTicksRequested,
00059 TInt aTicksInterval
00060 );
00061 static CTimedMessenger* NewL(const TDesC& aGreeting,
00062 TInt aTicksRequested,
00063 TInt aTicksInterval
00064 );
00065
00066 public:
00067
00068 void ConstructL(const TDesC& aGreeting,
00069 TInt aTicksRequested,
00070 TInt aTicksInterval
00071 );
00072
00073
00074 void IssueRequest();
00075
00076
00077
00078
00079 void DoCancel();
00080
00081
00082
00083
00084 void RunL();
00085
00086 public:
00087
00088 TBufC<20> iGreeting;
00089 TInt iTicksRequested;
00090
00091 TInt iTicksInterval;
00092 TInt iTicksDone;
00093 };
00094
00095
00097
00098
00099
00101 class CExampleScheduler : public CActiveScheduler
00102 {
00103 public:
00104 void Error (TInt aError) const;
00105 };
00106
00107
00109
00110
00111
00113 CTimedMessenger::CTimedMessenger()
00114 : CTimer(CActive::EPriorityStandard)
00115
00116 {};
00117
00118 CTimedMessenger* CTimedMessenger::NewLC(const TDesC& aGreeting,
00119 TInt aTicksRequested,
00120 TInt aTicksInterval
00121 )
00122 {
00123 CTimedMessenger* self=new (ELeave) CTimedMessenger;
00124 CleanupStack::PushL(self);
00125 self->ConstructL(aGreeting,aTicksRequested,aTicksInterval);
00126 return self;
00127 }
00128
00129 CTimedMessenger* CTimedMessenger::NewL(const TDesC& aGreeting,
00130 TInt aTicksRequested,
00131 TInt aTicksInterval
00132 )
00133 {
00134 CTimedMessenger* self = NewLC(aGreeting,aTicksRequested,aTicksInterval);
00135 CleanupStack::Pop();
00136 return self;
00137 }
00138
00139 void CTimedMessenger::ConstructL(const TDesC& aGreeting,
00140 TInt aTicksRequested,
00141 TInt aTicksInterval
00142 )
00143 {
00144
00145 CTimer::ConstructL();
00146
00147 iGreeting = aGreeting;
00148 iTicksRequested = aTicksRequested;
00149 iTicksInterval = aTicksInterval;
00150
00151 CActiveScheduler::Add(this);
00152 }
00153
00154
00155 CTimedMessenger::~CTimedMessenger()
00156 {
00157
00158 Cancel();
00159 }
00160
00161 void CTimedMessenger::DoCancel()
00162 {
00163
00164 CTimer::DoCancel();
00165
00166 iTicksDone = 0;
00167
00168 _LIT(KMsgCancelled,"Outstanding Messenger request cancelled\n");
00169 console->Printf(KMsgCancelled);
00170 }
00171
00172 void CTimedMessenger::IssueRequest()
00173 {
00174
00175 _LIT(KMsgAlreadyActive,"Is already Active");
00176 __ASSERT_ALWAYS(!IsActive(),User::Panic(KMsgAlreadyActive,EPanicAlreadyActive));
00177
00178 CTimer::After( iTicksInterval*1000000);
00179 }
00180
00181 void CTimedMessenger::RunL()
00182 {
00183
00184
00185 iTicksDone++;
00186
00187 _LIT(KFormatString,"%S \n");
00188 console->Printf(KFormatString,&iGreeting);
00189
00190 if (iTicksDone < iTicksRequested)
00191 {
00192 IssueRequest();
00193 }
00194 else
00195 {
00196 _LIT(KMsgFinished,"Messenger finished \n");
00197 console->Printf(KMsgFinished);
00198
00199 iTicksDone=0;
00200
00201 CActiveScheduler::Stop();
00202 }
00203 }
00204
00205
00207
00208
00209
00211 void CExampleScheduler::Error(TInt aError) const
00212 {
00213 _LIT(KMsgSchedErr,"CExampleScheduler-error");
00214 User::Panic(KMsgSchedErr,aError);
00215 }
00216
00217
00219
00220
00221
00223 LOCAL_C void doExampleL()
00224 {
00225
00226 CExampleScheduler* exampleScheduler = new (ELeave) CExampleScheduler;
00227
00228
00229 CleanupStack::PushL(exampleScheduler);
00230
00231
00232 CActiveScheduler::Install(exampleScheduler);
00233
00234
00235
00236 _LIT(KMsgExplanation,"A single CMessageTimer active object which runs until completion\n\n");
00237 console->Printf(KMsgExplanation);
00238 _LIT(KMsgGoodMorning,"Good Morning!");
00239 CTimedMessenger* myTimedMessage = CTimedMessenger::NewLC(KMsgGoodMorning,3,2);
00240
00241
00242 myTimedMessage->IssueRequest();
00243
00244
00245
00246 CActiveScheduler::Start();
00247
00248
00249
00250
00251 CleanupStack::PopAndDestroy(2);
00252 }
00253