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
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00047 #include "taskscheduler.h"
00048 #include <e32std.h>
00049 #include <schinfo.h>
00050 #include <e32base.h>
00051
00052 _LIT(KTaskName,"MyTaskName\n");
00053
00054 _LIT(KTitle, "Task Scheduler example");
00055 _LIT(KTextPressAKey, "\nPress any key to step through the example\n");
00056 _LIT(KExit,"\nPress any key to exit the application");
00057 _LIT(KPressAKey,"\nPress any key to continue\n");
00058
00059 _LIT(KConnect, "\nConnecting the client to the task scheduler server");
00060 _LIT(KRegisterClient,"\nRegistering the client\n");
00061 _LIT(KCreateSchedule,"Creating a persistent schedule\n");
00062 _LIT(KCreateTask,"Creating task for the schedule\n");
00063
00064 _LIT(KPersistentWait,"Waiting for the persistent task to complete. This will take 20 seconds\n");
00065 _LIT(KDone,"Task complete \n");
00066 _LIT(KTransientSchedule,"A transient schedule");
00067 _LIT(KTransientWait,"Waiting for the transient task to complete. This will take 20 seconds\n");
00068
00069 _LIT(KCreateTransient,"\nCreating a transient schedule with non-repeating task\n");
00070 _LIT(KDeleteAllTasks,"\nDeleting all the persistent tasks scheduled by this exe");
00071 _LIT(KDeleteAllSchedules,"\nDeleting all schedules created by this exe\n");
00072 _LIT(KTask, "Number of task(s) scheduled by us is(are) %d\n");
00073 _LIT(KExists, "The tasks scheduled exist\n");
00074 _LIT(KOtherTask,"Error! Unexpected schedules not scheduled by this exe exist\n");
00075
00080 CTaskSchedule* CTaskSchedule::NewLC()
00081 {
00082 CTaskSchedule* schedule = new(ELeave) CTaskSchedule();
00083 CleanupStack::PushL(schedule);
00084 schedule->ConstructL();
00085 return schedule;
00086 }
00087
00091 CTaskSchedule::CTaskSchedule()
00092 {
00093 }
00094
00095 void CTaskSchedule::ConstructL()
00096 {
00097 iConsole = Console::NewL(KTitle,TSize(KConsFullScreen,KConsFullScreen));
00098 iConsole->Printf (KTextPressAKey);
00099 iConsole->Getch ();
00100 }
00101
00105 CTaskSchedule::~CTaskSchedule()
00106 {
00107 iScheduler.Close();
00108 iConsole->Printf(KExit);
00109 iConsole->Getch();
00110
00111 delete iConsole;
00112 }
00113
00120 void CTaskSchedule::ConnectAndRegisterL()
00121 {
00122
00123
00124 iConsole->Printf(KConnect);
00125 User::LeaveIfError(iScheduler.Connect());
00126
00127 _LIT(KTaskExec,"Taskexecutor");
00128 TFileName filename;
00129 filename.Append(KTaskExec);
00130
00131
00132 iConsole->Printf(KRegisterClient);
00133
00134
00135 const TInt priority = 3;
00136 User::LeaveIfError(iScheduler.Register(filename, priority));
00137
00138 }
00139
00146 void CTaskSchedule::PersistentScheduleL()
00147 {
00148
00149 iConsole->Printf(KCreateSchedule);
00150 TSchedulerItemRef scheduleHandle;
00151 TTime time;
00152 time.UniversalTime();
00153
00154
00155
00156 time += TTimeIntervalSeconds(20);
00157
00158 TTsTime time2 (time, ETrue);
00159
00160 iConsole->Printf(KCreateTask);
00161 CreatePersistentScheduleL(scheduleHandle,time2);
00162
00163
00164 TTaskInfo taskInfo;
00165
00166 const TInt priority = 2;
00167 const TInt numberOfRepeats = 2;
00168
00169 taskInfo.iName = KTaskName;
00170
00171
00172
00173
00174 taskInfo.iPriority = priority;
00175
00176
00177 taskInfo.iRepeat = numberOfRepeats;
00178
00179 _LIT(KScheduleType," persistent schedule");
00180 const TDesC* persistent = &KScheduleType;
00181 HBufC* data = persistent->AllocLC();
00182
00183
00184 User::LeaveIfError(iScheduler.ScheduleTask(taskInfo, *data, scheduleHandle.iHandle));
00185
00186
00187 iConsole->Printf(KPersistentWait);
00188 const TInt twentySecs = 20000000;
00189 User::After(twentySecs);
00190
00191
00192 DeleteSchedulesL(scheduleHandle, EAllSchedules);
00193
00194 CleanupStack::PopAndDestroy(1);
00195
00196 }
00197
00198
00213 void CTaskSchedule::CreatePersistentScheduleL(TSchedulerItemRef& aRef, const TTsTime& aStartTime)
00214 {
00215 CArrayFixFlat<TScheduleEntryInfo2>* cSchEntryInfoArray;
00216 cSchEntryInfoArray = new CArrayFixFlat<TScheduleEntryInfo2>(1);
00217 CleanupStack::PushL(cSchEntryInfoArray);
00218
00219
00220 TScheduleEntryInfo2 entry1;
00221
00222
00223
00224 entry1.SetStartTime(aStartTime);
00225
00226
00227
00228 entry1.SetIntervalType(TIntervalType(EHourly));
00229
00230
00231
00232 const TInt eightHours = 480;
00233 entry1.SetValidityPeriod(TTimeIntervalMinutes (eightHours));
00234
00235
00236
00237 const TInt interval = 1;
00238 entry1.SetInterval(interval);
00239
00240 cSchEntryInfoArray->AppendL(entry1);
00241
00242 User::LeaveIfError(iScheduler.CreatePersistentSchedule(aRef, *cSchEntryInfoArray));
00243 CleanupStack::PopAndDestroy(cSchEntryInfoArray);
00244
00245 }
00246
00247
00256 void CTaskSchedule::CreateTransientScheduleL()
00257 {
00258 iConsole->ClearScreen();
00259
00260 TSchedulerItemRef ref;
00261 CArrayFixFlat<TScheduleEntryInfo2>* cSchEntryInfoArray;
00262 cSchEntryInfoArray = new CArrayFixFlat<TScheduleEntryInfo2>(1);
00263
00264 CleanupStack::PushL(cSchEntryInfoArray);
00265 ref.iName = KTransientSchedule;
00266 iConsole->Printf(KCreateTransient);
00267
00268
00269 TScheduleEntryInfo2 entry;
00270 TTime now;
00271
00272
00273 now.UniversalTime();
00274
00275
00276
00277 TInt offset = 15;
00278 now += TTimeIntervalSeconds(offset);
00279
00280
00281
00282 TTsTime time (now, ETrue);
00283
00284
00285
00286 entry.SetStartTime(time);
00287
00288
00289 entry.SetIntervalType(TIntervalType(EHourly));
00290
00291
00292
00293 TInt validity = 120;
00294 entry.SetValidityPeriod(TTimeIntervalMinutes (validity));
00295
00296
00297
00298 entry.SetInterval(1);
00299 cSchEntryInfoArray->AppendL(entry);
00300
00301
00302 TTaskInfo taskInfo;
00303
00304 taskInfo.iName = KTransientSchedule;
00305
00306 const TInt tId = 0;
00307 taskInfo.iTaskId = tId;
00308
00309
00310 const TInt numberOfRepeats = 1;
00311 taskInfo.iRepeat = numberOfRepeats;
00312
00313
00314
00315
00316 const TInt priority = 2;
00317 taskInfo.iPriority = priority;
00318
00319 _LIT(KScheduleType," transient schedule");
00320 const TDesC* transient = &KScheduleType;
00321 HBufC* data = transient->AllocLC();
00322
00323
00324 User::LeaveIfError(iScheduler.ScheduleTask(taskInfo, *data, ref, *cSchEntryInfoArray));
00325
00326
00327 offset = 19;
00328 now += TTimeIntervalSeconds(offset);
00329 TTsTime newTime (now, ETrue);
00330 entry.SetStartTime(newTime);
00331 validity = 300;
00332 entry.SetValidityPeriod(TTimeIntervalMinutes (validity));
00333 cSchEntryInfoArray->AppendL(entry);
00334
00335
00336 User::LeaveIfError(iScheduler.EditSchedule(ref.iHandle, *cSchEntryInfoArray));
00337
00338 CleanupStack::PopAndDestroy(2);
00339
00340
00341 TBool exists;
00342 DoesScheduledItemExistL(ref, exists);
00343 if(!exists)
00344 {
00345 User::Leave(KErrNotFound);
00346 }
00347
00348 iConsole->Printf(KTransientWait);
00349 const TInt twentySecs = 20000000;
00350 User::After(twentySecs);
00351
00352 iConsole->Printf(KDone);
00353
00354
00355
00356 DoesScheduledItemExistL(ref, exists);
00357 if(exists)
00358 {
00359 User::Leave(KErrGeneral);
00360 }
00361 }
00362
00369 void CTaskSchedule::DoesScheduledItemExistL(TSchedulerItemRef &aRef, TBool& aExists)
00370
00371 {
00372 aExists = EFalse;
00373 CArrayFixFlat<TSchedulerItemRef>* CSchItemRefArray;
00374
00375 CSchItemRefArray = new CArrayFixFlat<TSchedulerItemRef>(3);
00376 CleanupStack::PushL(CSchItemRefArray);
00377
00378 User::LeaveIfError(iScheduler.GetScheduleRefsL(*CSchItemRefArray, EAllSchedules));
00379
00380 TInt count = CSchItemRefArray->Count();
00381 for(TInt i = 0; i < count; i++)
00382 {
00383
00384 if(aRef.iHandle == (*CSchItemRefArray)[i].iHandle)
00385 {
00386 aExists = ETrue;
00387 iConsole->Printf(KExists);
00388 iConsole->Printf(KTask, count);
00389 }
00390 else
00391 {
00392 iConsole->Printf(KOtherTask);
00393 }
00394 }
00395
00396 CleanupStack::PopAndDestroy();
00397
00398 }
00399
00411 void CTaskSchedule::DeleteSchedulesL(TSchedulerItemRef &aRef, TScheduleFilter aFilter)
00412 {
00413 CArrayFixFlat<TSchedulerItemRef>* CSchItemRefArray;
00414
00415 CSchItemRefArray = new CArrayFixFlat<TSchedulerItemRef>(3);
00416 CleanupStack::PushL(CSchItemRefArray);
00417
00418 User::LeaveIfError(iScheduler.GetScheduleRefsL(*CSchItemRefArray, aFilter));
00419
00420 TInt count = CSchItemRefArray->Count();
00421 iConsole->Printf(KTask, count);
00422
00423 for(TInt i = 0; i < count; i++)
00424 {
00425
00426
00427 if(aRef.iHandle == (*CSchItemRefArray)[i].iHandle)
00428 {
00429
00430 iConsole->Printf(KDeleteAllTasks);
00431 User::LeaveIfError(iScheduler.DeleteTask(aRef.iHandle));
00432
00433 iConsole->Printf(KDeleteAllSchedules);
00434 User::LeaveIfError(iScheduler.DeleteSchedule(aRef.iHandle));
00435
00436 iConsole->Printf(KPressAKey);
00437 iConsole->Getch();
00438
00439 }
00440 else
00441 {
00442
00443
00444 iConsole->Printf(KOtherTask);
00445 }
00446 }
00447 CleanupStack::PopAndDestroy();
00448
00449 }
00450
00451 void MainL()
00452 {
00453
00454 CActiveScheduler* scheduler = new (ELeave) CActiveScheduler;
00455 CleanupStack::PushL(scheduler);
00456
00457
00458 CActiveScheduler::Install( scheduler );
00459 CTaskSchedule* app = CTaskSchedule::NewLC();
00460
00461
00462 app->ConnectAndRegisterL();
00463
00464
00465 app->PersistentScheduleL();
00466
00467
00468 app->CreateTransientScheduleL();
00469
00470 CleanupStack::PopAndDestroy(2);
00471
00472 }
00473
00474 TInt E32Main()
00475 {
00476 __UHEAP_MARK;
00477 CTrapCleanup* cleanup = CTrapCleanup::New();
00478 if(cleanup == NULL)
00479 {
00480 return KErrNoMemory;
00481 }
00482
00483 TRAPD(err, MainL());
00484 if(err != KErrNone)
00485 {
00486 User::Panic(_L("Failed to complete"),err);
00487 }
00488 delete cleanup;
00489
00490 __UHEAP_MARKEND;
00491 return KErrNone;
00492 }