×
Namespaces

Variants
Actions
(Difference between revisions)

Archived:Change a synchronous Symbian C++ method to act like an asynchronous one

From Nokia Developer Wiki
Jump to: navigation, search
tepaa (Talk | contribs)
hamishwillee (Talk | contribs)
m (Hamishwillee - Bot update - Fix links)
 
(12 intermediate revisions by 7 users not shown)
Line 1: Line 1:
__NOTOC__
+
{{Archived|timestamp=20120313124756|user=roy.debjit| }}
__NOEDITSECTION__
+
[[Category:Symbian C++]][[Category:Code Snippet]][[Category:S60 3rd Edition (initial release)]][[Category:Code Snippet]]
 
+
{{ArticleMetaData <!-- v1.2 -->
{|style="background:#eceff2" width="660px" border="1" cellpadding="5" cellspacing="0"
+
|sourcecode= <!-- Link to example source code (e.g. [[Media:The Code Example ZIP.zip]]) -->
|-
+
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
|'''ID''' ||
+
|devices= Nokia N95
|'''Creation date''' || May 27, 2008
+
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) -->
|-
+
|platform= S60 3rd Edition, MR
|'''Platform''' || S60 3rd Edition, MR
+
|devicecompatability= <!-- Compatible devices (e.g.: All* (must have GPS) ) -->
|'''Tested on devices''' || Nokia N95
+
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
|-
+
|signing= <!-- Empty or one of Self-Signed, DevCert, Manufacturer -->
|'''Category''' || Symbian C++
+
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
|'''Subcategory''' || Code Examples
+
|keywords= CActiveSchedulerWait, CActive, TRequestStatus
|-
+
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
|}
+
|translated-by= <!-- [[User:XXXX]] -->
 
+
|translated-from-title= <!-- Title only -->
 
+
|translated-from-id= <!-- Id of translated revision -->
{|style="background:#eceff2" width="660px" border="1" cellpadding="5" cellspacing="0"
+
|review-by= <!-- After re-review: [[User:username]] -->
|-
+
|review-timestamp= <!-- After re-review: YYYYMMDD -->
|'''Keywords (APIs, classes, methods, functions):''' CActiveSchedulerWait, CActive, TRequestStatus
+
|update-by= <!-- After significant update: [[User:username]]-->
|}
+
|update-timestamp= <!-- After significant update: YYYYMMDD -->
 
+
|creationdate= 20080527
 +
|author= [[User:Tepaa]]
 +
<!-- The following are not in current metadata -->
 +
|subcategory= Code Examples
 +
|id= CS001010
 +
}}
 +
 
 
==Overview==
 
==Overview==
This code snippet shows how to change synchronous method act like asynchronous.
+
This code snippet shows how to modify a synchronous method to behave like an asynchronous method.  
  
There is two different method:
+
There are two different methods:
* void LongRunningTaskL(TInt& aResult) is synchronous method that act like asynchronous but returns when task done
+
* {{Icode|void LongRunningTaskL(TInt& aResult)}} is a synchronous method that acts like an asynchronous one but returns when the task has been completed.
* void LongRunningTaskL(MMySynchronousTaskDone* aObserver) is synchronous method that act like asynchronous. Note that method returns right after it has been started and notifies task completion via MMySynchronousTaskDone
+
* {{Icode|void LongRunningTaskL(MMySynchronousTaskDone* aObserver)}} is a synchronous method that acts like an asynchronous method. Note that the method returns right after it has been started and notifies of task completion via {{Icode|MMySynchronousTaskDone}}.
  
 
==MMP file==
 
==MMP file==
 
The following capabilities and libraries are required:
 
The following capabilities and libraries are required:
 
<code cpp>
 
<code cpp>
CAPABILITY   None
+
CAPABILITY None
LIBRARY       euser.lib
+
LIBRARY euser.lib
 
</code>
 
</code>
  
Line 62: Line 68:
  
 
     public:
 
     public:
         // Synchronous method that act like asynchronous
+
         // Synchronous method that acts like an asynchronous one
         // but returns when task done.
+
         // but returns when task completed.
 
         void LongRunningTaskL(TInt& aResult);
 
         void LongRunningTaskL(TInt& aResult);
 
          
 
          
         // Synchronous method that act like asynchronous
+
         // Synchronous method that acts like an asynchronous one
         // note that method returns right after it has been started
+
         // note that the method returns right after it has been started
         // and notifies task completion via MMySynchronousTaskDone
+
         // and notifies of task completion via MMySynchronousTaskDone
 
         void LongRunningTaskL(MMySynchronousTaskDone* aObserver);
 
         void LongRunningTaskL(MMySynchronousTaskDone* aObserver);
 
          
 
          
Line 83: Line 89:
 
          
 
          
 
     private:
 
     private:
         EState                 iRunningState;
+
         EState iRunningState;
         CActiveSchedulerWait   iSchedulerWait;
+
         CActiveSchedulerWaitiSchedulerWait;
         TInt                   iSomeLongRunningTask;
+
         TInt iSomeLongRunningTask;
 
         MMySynchronousTaskDone* iObserver;
 
         MMySynchronousTaskDone* iObserver;
 
     };
 
     };
Line 108: Line 114:
 
     {
 
     {
 
     CActiveScheduler::Add(this);
 
     CActiveScheduler::Add(this);
 +
    iSchedulerWait = new (ELeave) CActiveSchedulerWait();
 
     }
 
     }
  
Line 113: Line 120:
 
     {
 
     {
 
     Cancel();
 
     Cancel();
 +
    delete iSchedulerWait;
 
     }
 
     }
  
Line 124: Line 132:
  
 
         GiveTimeToOthers();
 
         GiveTimeToOthers();
         iSchedulerWait.Start();  
+
         iSchedulerWait->Start();  
  
 
         // Continues here after CActiveSchedulerWait::AsyncComplete() is called
 
         // Continues here after CActiveSchedulerWait::AsyncComplete() is called
Line 139: Line 147:
 
     }
 
     }
  
// This method returns right after synchronous task is started
+
// This method returns right after the synchronous task is started
 
void CMySynchronousClass::LongRunningTaskL(MMySynchronousTaskDone* aObserver)
 
void CMySynchronousClass::LongRunningTaskL(MMySynchronousTaskDone* aObserver)
 
     {
 
     {
Line 158: Line 166:
 
void CMySynchronousClass::DoLongRunningTask()
 
void CMySynchronousClass::DoLongRunningTask()
 
     {
 
     {
     // TODO: You must execute synchronous method in small steps and
+
     // TODO: Here is a synchronous method call.
     // call GiveTimeToOthers() in between steps
+
     //       Try to split call in small parts.
 
     iSomeLongRunningTask++;
 
     iSomeLongRunningTask++;
 
     }
 
     }
Line 221: Line 229:
 
                 iObserver->TaskDone(iSomeLongRunningTask);
 
                 iObserver->TaskDone(iSomeLongRunningTask);
 
                 }
 
                 }
             if (iSchedulerWait.IsStarted())
+
             if (iSchedulerWait->IsStarted())
 
                 {
 
                 {
                 iSchedulerWait.AsyncStop();
+
                 iSchedulerWait->AsyncStop();
 
                 }
 
                 }
 
             break;
 
             break;
Line 229: Line 237:
 
         default:
 
         default:
 
             {
 
             {
             if (iSchedulerWait.IsStarted())
+
             if (iSchedulerWait->IsStarted())
 
                 {
 
                 {
                 iSchedulerWait.AsyncStop();
+
                 iSchedulerWait->AsyncStop();
 
                 }
 
                 }
 
             break;
 
             break;
Line 253: Line 261:
 
         iObserver->TaskDone(KErrCancel);
 
         iObserver->TaskDone(KErrCancel);
 
         }
 
         }
     if (iSchedulerWait.IsStarted())
+
     if (iSchedulerWait->IsStarted())
 
         {
 
         {
         iSchedulerWait.AsyncStop();
+
         iSchedulerWait->AsyncStop();
 
         }
 
         }
 
     }
 
     }
Line 261: Line 269:
  
 
==Postconditions==
 
==Postconditions==
Synchronous methods act a like asynchronous.
+
The synchronous method acts like an asynchronous one.
  
 
==See also==
 
==See also==
[[Using_CActiveSchedulerWait]]
+
[[Archived:Using CActiveSchedulerWait]]
 
+
[[Change_asynchronous_method_into_synchronous]]
+
  
[[Category:Symbian C++]][[Category:Code Examples]]
+
[[Archived:Changing asynchronous method to synchronous in Symbian C++]]

Latest revision as of 03:30, 29 June 2012

Archived.pngArchived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.

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: tepaa (27 May 2008)
Last edited: hamishwillee (29 Jun 2012)

Contents

[edit] Overview

This code snippet shows how to modify a synchronous method to behave like an asynchronous method.

There are two different methods:

  • void LongRunningTaskL(TInt& aResult) is a synchronous method that acts like an asynchronous one but returns when the task has been completed.
  • void LongRunningTaskL(MMySynchronousTaskDone* aObserver) is a synchronous method that acts like an asynchronous method. Note that the method returns right after it has been started and notifies of task completion via MMySynchronousTaskDone.

[edit] MMP file

The following capabilities and libraries are required:

CAPABILITY None
LIBRARY euser.lib

[edit] Header

#include <e32base.h>
 
class MMySynchronousTaskDone
{
public:
virtual void TaskDone(TInt aRet) = 0;
};
 
class CMySynchronousClass : public CActive
{
enum EState
{
EIdle = 0,
EStarted,
ERunningTask1,
ERunningTask2,
ERunningTask3,
EEnd
};
 
public:
static CMySynchronousClass* NewL();
virtual ~CMySynchronousClass();
 
public:
// Synchronous method that acts like an asynchronous one
// but returns when task completed.
void LongRunningTaskL(TInt& aResult);
 
// Synchronous method that acts like an asynchronous one
// note that the method returns right after it has been started
// and notifies of task completion via MMySynchronousTaskDone
void LongRunningTaskL(MMySynchronousTaskDone* aObserver);
 
private: // From CActive
void RunL();
TInt RunError(TInt aError);
void DoCancel();
 
private:
CMySynchronousClass();
void ConstructL();
void GiveTimeToOthers();
void DoLongRunningTask();
 
private:
EState iRunningState;
CActiveSchedulerWait* iSchedulerWait;
TInt iSomeLongRunningTask;
MMySynchronousTaskDone* iObserver;
};

[edit] Source

CMySynchronousClass* CMySynchronousClass::NewL()
{
CMySynchronousClass* self = new (ELeave) CMySynchronousClass();
CleanupStack::PushL( self );
self->ConstructL();
CleanupStack::Pop();
return self;
}
 
CMySynchronousClass::CMySynchronousClass():CActive(EPriorityStandard)
{
}
 
void CMySynchronousClass::ConstructL()
{
CActiveScheduler::Add(this);
iSchedulerWait = new (ELeave) CActiveSchedulerWait();
}
 
CMySynchronousClass::~CMySynchronousClass()
{
Cancel();
delete iSchedulerWait;
}
 
// This method returns after synchronous task completion
void CMySynchronousClass::LongRunningTaskL(TInt& aResult)
{
if (iRunningState == EIdle)
{
iSomeLongRunningTask = 0;
iRunningState = EStarted;
 
GiveTimeToOthers();
iSchedulerWait->Start();
 
// Continues here after CActiveSchedulerWait::AsyncComplete() is called
if (iStatus.Int() != KErrNone)
{
User::Leave(iStatus.Int());
}
aResult = iSomeLongRunningTask;
}
else
{
User::Leave(KErrNotReady);
}
}
 
// This method returns right after the synchronous task is started
void CMySynchronousClass::LongRunningTaskL(MMySynchronousTaskDone* aObserver)
{
if (iRunningState == EIdle)
{
iObserver = aObserver;
iSomeLongRunningTask = 0;
iRunningState = EStarted;
GiveTimeToOthers();
}
else
{
User::Leave(KErrNotReady);
}
 
}
 
void CMySynchronousClass::DoLongRunningTask()
{
// TODO: Here is a synchronous method call.
// Try to split call in small parts.
iSomeLongRunningTask++;
}
 
void CMySynchronousClass::GiveTimeToOthers()
{
TRequestStatus* status = &iStatus;
User::RequestComplete( status, KErrNone );
SetActive();
}
 
void CMySynchronousClass::RunL()
{
switch (iRunningState)
{
case EIdle:
{
break;
}
case EStarted:
{
iRunningState = ERunningTask1;
// TODO: Call synchronous method
DoLongRunningTask();
 
GiveTimeToOthers();
break;
}
case ERunningTask1:
{
iRunningState = ERunningTask2;
// TODO: Call synchronous method
DoLongRunningTask();
 
GiveTimeToOthers();
break;
}
case ERunningTask2:
{
iRunningState = ERunningTask3;
// TODO: Call synchronous method
DoLongRunningTask();
 
GiveTimeToOthers();
break;
}
case ERunningTask3:
{
iRunningState = EEnd;
// TODO: Do some end processing if needed
 
GiveTimeToOthers();
break;
}
case EEnd:
{
// Done
iRunningState = EIdle;
if (iObserver)
{
iObserver->TaskDone(iSomeLongRunningTask);
}
if (iSchedulerWait->IsStarted())
{
iSchedulerWait->AsyncStop();
}
break;
}
default:
{
if (iSchedulerWait->IsStarted())
{
iSchedulerWait->AsyncStop();
}
break;
}
};
}
 
TInt CMySynchronousClass::RunError(TInt aError)
{
if (iObserver)
{
iObserver->TaskDone(aError);
}
return KErrNone;
}
 
void CMySynchronousClass::DoCancel()
{
if (iObserver)
{
iObserver->TaskDone(KErrCancel);
}
if (iSchedulerWait->IsStarted())
{
iSchedulerWait->AsyncStop();
}
}

[edit] Postconditions

The synchronous method acts like an asynchronous one.

[edit] See also

Archived:Using CActiveSchedulerWait

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

This page was last modified on 29 June 2012, at 03:30.
121 page views in the last 30 days.
×