Splitting long running tasks with active objects
Article Metadata
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);
void ConstructL(void);
CSimpleAO(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;
};


14 Dec
2009
The mechanism of active objects is one of the main cornerstones of Symbian C++. With the help of this paradigm you can easily and elegancy organize cooperative multitasking in the one thread. This article demonstrates the implementation of useful and frequently used in practice approach: splitting a long operation into a sequence of asynchronous calls using active objects.
With the help of such approach developers can perform resource-intensive and time-consuming operations without the use of additional threads, which is very important especially for mobile applications. Another plus is the standart user interface S60 is also realized using active objects. Thus, this approach is quite applicable in GUI applications - response time of graphical user interface elements will remain at a comfortable level.
02 Sep
2009
It should be noted that use of the proposed mechanism requires a deep understanding of how active objects work. Beginners shoul clearly understand this paradigm before using this article in practice.
Amrok90 -
Should it not be
}}
Hamishwillee - I don't think so
As I understand it if you call Cancel() then it calls doCancel() and checks for completion. So the RunL() would not be called in this case and adding the check would be pointless. I can't explain the purpose of iCancel.
I could be wrong in my understanding though.hamishwillee 05:51, 15 December 2011 (EET)