You Are Here:

Community: Wiki

This page was last modified on 1 September 2009, at 20:00.

Splitting long running tasks with active objects

From Forum Nokia Wiki

Reviewer Approved   

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 Wiki Articles

No related wiki articles found

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qdcZidentifierQSxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2findeE78E2ephpE2fJ2ME45E5fE52SSE5fParserE5fwithE5fKE58mlX qdcZpublisherQUxhttpE3aE2fE2fswE2enokiaE2ecomE2fidE2fc764fd1cE2d8b06E2d499aE2d9a6aE2d17c3903d5a65E2fforumE5fnokiaE5fcrawlerE5fagentX qdcZtitleQSxJ2ME45E20E52SSE20ParserE20withE20KE58mlE20E2dE20ForumE20NokiaE20WikiX qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfntypeZE52esourceQ qdcZtypeQUqfntypeZWebpageQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qrssZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qfnZdistributionQUxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2fX qfnZtypeQUqfntypeZE52esourceQ qfnZtypeQUqfntypeZWebpageQ qfnZupdatedQDx2008E2d10E2d02X qmarsZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfntypeZE52esourceQ qrdfZtypeQUqfntypeZWebpageQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ
User Rating: qfnZuserE5FratingQNx1E2E0000X