Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.
Splitting long running tasks with active objects
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.
CSimpleAO* CSimpleAO::NewL(MSimpleAOObserver& aObserver)
CSimpleAO* self = NewLC(aObserver);
CSimpleAO* CSimpleAO::NewLC(MSimpleAOObserver& aObserver)
CSimpleAO* self = new (ELeave) CSimpleAO(aObserver);
iRounds = 100;
iError = KErrNone;
iCancel = ETrue;
iError = iStatus.Int();
if(iError != KErrNone || (iCurrent > iRounds))
TInt Err = DoOneRound();
// do one step on the process in here and update variables;
public: // New methods
virtual void MeProcessL(TInt aNow,TInt aFull) = 0;
virtual void MeFinnishL(CSimpleAO* aObject, TInt aError) = 0;
class CSimpleAO : public CActive
static CSimpleAO* NewL(MSimpleAOObserver& aObserver);
static CSimpleAO* NewLC(MSimpleAOObserver& aObserver);