×
Namespaces

Variants
Actions
(Difference between revisions)

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

From Nokia Developer Wiki
Jump to: navigation, search
Forum Nokia KB (Talk | contribs)
extkbeditor1 (Talk | contribs)
m
Line 2: Line 2:
 
__NOEDITSECTION__
 
__NOEDITSECTION__
 
{{KBCS}}
 
{{KBCS}}
{|style="background:#eceff2" width="660px" border="1" cellpadding="5" cellspacing="0"
+
{{CodeSnippet
|-
+
|id=CS000983
|'''ID''' ||CS000983
+
|platform=S60 3rd Edition, MR
|'''Creation date''' || May 28, 2008
+
|devices=Nokia N95
|-
+
|category=Symbian C++
|'''Platform''' || S60 3rd Edition, MR
+
|subcategory=Code Examples
|'''Tested on devices''' || Nokia N95
+
|creationdate=May 28, 2008
|-
+
|keywords=CActiveSchedulerWait, CActive, TRequestStatus
|'''Category''' || Symbian C++
+
}}
|'''Subcategory''' || Code Examples
+
|-
+
|}
+
 
+
 
+
{|style="background:#eceff2" width="660px" border="1" cellpadding="5" cellspacing="0"
+
|-
+
|'''Keywords (APIs, classes, methods, functions):''' CActiveSchedulerWait, CActive, TRequestStatus
+
|}
+
  
 
==Overview==
 
==Overview==

Revision as of 16:05, 12 October 2008


Template:KBCS

Article Metadata
Tested with
Devices(s): Nokia N95
Compatibility
Platform(s): S60 3rd Edition, MR
S60 3rd Edition (initial release)
Article
Keywords: CActiveSchedulerWait, CActive, TRequestStatus
Created: (28 May 2008)
Last edited: extkbeditor1 (12 Oct 2008)

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 CS000982 - 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.

See also

CS000982 - Using CActiveSchedulerWait

121 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×