×
Namespaces

Variants
Actions

Splitting long running tasks with active objects

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Article
Created: symbianyucca (20 Mar 2007)
Last edited: hamishwillee (11 Jan 2012)

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;
};
This page was last modified on 11 January 2012, at 09:38.
42 page views in the last 30 days.
×