×
Namespaces

Variants
Actions
Revision as of 09:48, 21 May 2008 by tepaa (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Archived:Changing asynchronous method to synchronous in Symbian C++

From Nokia Developer Wiki
Jump to: navigation, search


ID Creation date May 21, 2008
Platform S60 3rd Edition, MR Tested on devices Nokia N95
Category Symbian C++ Subcategory Code Examples


Keywords (APIs, classes, methods, functions): CActiveSchedulerWait, CActive, TRequestStatus

Overview

This code snippet shows how to change some asynchronous method interface into synchronous. This can be done with CActiveSchedulerWait.

There is two different synchronous methods:

  • CMyClass::DoAsynchronousL() uses CActiveSchedulerWait for waiting asynchronous method that completes into CMyClass::AsyncComplete()
  • CMyClass::DoAsynchronousWithStatusL() uses CActiveSchedulerWait for waiting asynchronous method that has TRequestStatus in its interface. Method completes into CMyClass::RunL().

See more about using CActiveSchedulerWait from Using_CActiveSchedulerWait

MMP file

The following capabilities and libraries are required:

CAPABILITY    None
LIBRARY euser.lib

Header

#include <e32base.h>
 
// for CSomeAsyncClass
class MAsynchIF
{
public:
void AsyncComplete(TInt aResult);
};
 
class CMyClass : public CActive
{
public:
static CMyClass* NewLC();
virtual ~CMyClass();
 
// Synchronous methods
TInt DoAsynchronousL();
TInt DoAsynchronousWithStatusL();
 
pritave: // From MAsynchIF (CSomeAsyncClass)
void AsyncComplete(TInt aResult);
 
private: // From CActive
void RunL();
void DoCancel();
 
private:
CSomeAsyncClass* iSomeAsyncClass;
CActiveSchedulerWait iSchedulerWait;
TInt iResult;
};

Source

CMyClass* CMyClass::NewLC()
{
CMyClass* self = new (ELeave) CMyClass();
CleanupStack::PushL(self);
self->ConstructL();
return self;
}
 
CMyClass::CMyClass()
: CActive(CActive::EPriorityStandard)
{
}
 
void CMyClass::ConstructL()
{
CActiveScheduler::Add(this);
iSomeAsyncClass = CSomeAsyncClass::NewL(this);
}
 
CMyClass::~CMyClass()
{
Cancel();
delete iSomeAsyncClass;
}
 
// This is synchronous method even it call some asynchronous interface that
// completes into CMyClass::AsyncComplete()
TInt CMyClass::DoAsynchronousL()
{
User::LeaveIfError(iSomeAsyncClass->SomeAsynchronousIfL());
 
iSchedulerWait.Start();
// Continues here after CMyClass::AsyncComplete() is called
// by CSomeAsyncClass.
 
return iResult;
}
 
void CMyClass::AsyncComplete(TInt aResult)
{
iResult = aResult;
iSchedulerWait.AsyncStop();
}
 
// This is synchronous method even it call some asynchronous interface that
// completes into CMyClass::RunL() because it uses TRequestStatus as
// a parameter
Int CMyClass::DoAsynchronousWithStatusL()
{
if (!IsActive())
{
User::LeaveIfError(iSomeAsyncClass->SomeAsynchronousIfL(iStatus));
SetActive();
 
iSchedulerWait.Start();
// Continues here after CMyClass::RunL() is called
// by CSomeAsyncClass.
}
else
{
User::Leave(KErrNotReady);
}
return iResult;
}
 
void CMyClass::RunL()
{
// This is called when DoAsynchronousWithStatusL() completes
iResult = iStatus.Int();
iSchedulerWait.AsyncStop();
}
 
void CMyClass::DoCancel()
{
// This cancels DoAsynchronousWithStatusL() method
iResult = KErrCancel;
iSchedulerWait.AsyncStop();
}

Postconditions

Asynchronous methods look a like synchronous.

See also

Using_CActiveSchedulerWait

135 page views in the last 30 days.
×