Archived:Changing asynchronous method to synchronous in Symbian C++
| Line 22: | Line 22: | ||
==Overview== | ==Overview== | ||
| − | This code snippet shows how to change | + | This code snippet shows how to change an asynchronous method interface to a |
| − | synchronous. This can be done with CActiveSchedulerWait. | + | synchronous method. This can be done with CActiveSchedulerWait. |
| − | There | + | There are two different synchronous methods: |
| − | * CMyClass::DoAsynchronousL() uses CActiveSchedulerWait for waiting asynchronous method that completes | + | * CMyClass::DoAsynchronousL() uses CActiveSchedulerWait for waiting for the asynchronous method that completes with CMyClass::AsyncComplete() |
| − | * CMyClass::DoAsynchronousWithStatusL() uses CActiveSchedulerWait for waiting asynchronous method that has TRequestStatus in its interface. | + | * CMyClass::DoAsynchronousWithStatusL() uses CActiveSchedulerWait for waiting for the asynchronous method that has TRequestStatus in its interface. The method completes with CMyClass::RunL(). |
| − | + | For more information on using CActiveSchedulerWait, see article [[Using CActiveSchedulerWait]] | |
==MMP file== | ==MMP file== | ||
| Line 100: | Line 100: | ||
} | } | ||
| − | // This is synchronous method even it | + | // This is a synchronous method even though it calls an asynchronous interface that |
| − | // completes | + | // completes with CMyClass::AsyncComplete() |
TInt CMyClass::DoAsynchronousL() | TInt CMyClass::DoAsynchronousL() | ||
{ | { | ||
| Line 119: | Line 119: | ||
} | } | ||
| − | // This is synchronous method even it | + | // This is an synchronous method even though it calls an asynchronous interface that |
| − | // completes | + | // completes with CMyClass::RunL() because it uses TRequestStatus as |
// a parameter | // a parameter | ||
TInt CMyClass::DoAsynchronousWithStatusL() | TInt CMyClass::DoAsynchronousWithStatusL() | ||
| Line 149: | Line 149: | ||
void CMyClass::DoCancel() | void CMyClass::DoCancel() | ||
{ | { | ||
| − | // This cancels DoAsynchronousWithStatusL() method | + | // This cancels the DoAsynchronousWithStatusL() method |
iResult = KErrCancel; | iResult = KErrCancel; | ||
iSchedulerWait.AsyncStop(); | iSchedulerWait.AsyncStop(); | ||
| Line 156: | Line 156: | ||
==Postconditions== | ==Postconditions== | ||
| − | + | The asynchronous methods looks like a synchronous method. | |
==See also== | ==See also== | ||
| − | [[ | + | [[Using CActiveSchedulerWait]] |
| − | [[Category:Symbian C++]][[Category:Code Examples]] | + | [[Category:Symbian C++]][[Category:Code Examples]][[Category:S60 3rd Edition]] |
Revision as of 08:54, 28 May 2008
| 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 an asynchronous method interface to a synchronous method. This can be done with CActiveSchedulerWait.
There are two different synchronous methods:
- CMyClass::DoAsynchronousL() uses CActiveSchedulerWait for waiting for the asynchronous method that completes with CMyClass::AsyncComplete()
- CMyClass::DoAsynchronousWithStatusL() uses CActiveSchedulerWait for waiting for the asynchronous method that has TRequestStatus in its interface. The method completes with CMyClass::RunL().
For more information on using CActiveSchedulerWait, see article 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, MAsynchIF
{
public:
static CMyClass* NewLC();
virtual ~CMyClass();
// Synchronous methods
TInt DoAsynchronousL();
TInt DoAsynchronousWithStatusL();
private: // 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 a synchronous method even though it calls an asynchronous interface that
// completes with 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 an synchronous method even though it calls an asynchronous interface that
// completes with CMyClass::RunL() because it uses TRequestStatus as
// a parameter
TInt 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 the DoAsynchronousWithStatusL() method
iResult = KErrCancel;
iSchedulerWait.AsyncStop();
}
Postconditions
The asynchronous methods looks like a synchronous method.

