This page was last modified 11:12, 12 December 2007.
Splitting long running tasks with active objects
From Forum Nokia Wiki
CSimpleAO example code illustrates a framework that could be used to split long running tasks with active objects. It can only be used with processes that can be divided into steps, and in it each step would then be handled inside DoOneRound-function.
Important aspects when using active objects is that the priority has to be given while constructing the active object, also before using it it should always be added to the CActiveScheduler, as shown in ConstructL-function. Also always when any asynchronous function is started SetActive-function must be called.
Note that the priority value for the active object should always be kept as small as possible, this is important since if the priority value is set too high, the long runnig task could still be blocking other event handlers that have lower priority.
When extending this example code, all initialization can be added to the StartProcess-function. and each process step handling code can be added to the DoOneRound-function.
SimpleAO.cpp
CSimpleAO* CSimpleAO::NewL(MSimpleAOObserver& aObserver) { CSimpleAO* self = NewLC(aObserver); CleanupStack::Pop(self); return self; } CSimpleAO* CSimpleAO::NewLC(MSimpleAOObserver& aObserver) { CSimpleAO* self = new (ELeave) CSimpleAO(aObserver); CleanupStack::PushL(self); self->ConstructL(); return self; } CSimpleAO::CSimpleAO(MSimpleAOObserver& aObserver) :CActive(0),iObserver(aObserver) { } CSimpleAO::~CSimpleAO() { Cancel(); } void CSimpleAO::ConstructL(void) { CActiveScheduler::Add(this); } void CSimpleAO::StartProcess(void) { iRounds = 100; iCurrent= 0; iError = KErrNone; iCancel= EFalse; TRequestStatus* status=&iStatus; User::RequestComplete(status, KErrNone); SetActive(); } void CSimpleAO::DoCancel() { iCancel = ETrue; } void CSimpleAO::RunL() { iError = iStatus.Int(); if(iError != KErrNone || (iCurrent > iRounds)) { FinnishedL(); } else { iObserver.MeProcessL(iCurrent,iRounds); TInt Err = DoOneRound(); TRequestStatus* status=&iStatus; User::RequestComplete(status, Err); SetActive(); } } TInt CSimpleAO::DoOneRound(void) { TInt RetError(KErrNone); // do one step on the process in here and update variables; iCurrent++; return RetError; } void CSimpleAO::FinnishedL(void) { iObserver.MeFinnishL(this,iError); }
SimpleAO.h
#include <e32base.h> #include <F32FILE.H> class MSimpleAOObserver { public: // New methods virtual void MeProcessL(TInt aNow,TInt aFull) = 0; virtual void MeFinnishL(CSimpleAO* aObject, TInt aError) = 0; }; class CSimpleAO : public CActive { public: static CSimpleAO* NewL(MSimpleAOObserver& aObserver); static CSimpleAO* NewLC(MSimpleAOObserver& aObserver); ~CSimpleAO(); void StartProcess(void); protected: void DoCancel(); void RunL(); private: TInt DoOneRound(void); void FinnishedL(void); private: MSimpleAOObserver& iObserver; TBool iCancel; TInt iCurrent,iRounds,iError; };
| Related Discussions | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Trick to prevent Python from closing on red key press. | y.a.k | Python | 14 | 2008-06-29 12:00 |
| application crashes at CActiveScheduler::Start() | Gill | Symbian User Interface | 4 | 2007-06-06 09:14 |
| How to Scroll text horizontally in a listbox cell | lfd | Symbian User Interface | 3 | 2007-06-07 13:16 |
| Nokia: which phones have compacting GC, and which don't? | niekvs | Mobile Java General | 12 | 2003-12-13 16:57 |
| Win32 PeekMessage analogue | assd | General Symbian C++ | 2 | 2002-11-04 06:37 |
