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