×
Namespaces

Variants
Actions

将一个同步方法改为如异步方式一样执行

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata

文章
hoolee 在 02 Sep 2008 创建
最后由 hamishwillee 在 15 Dec 2011 编辑

CS001010


  • 开发伙伴平台:

S60 3rd Edition, MR


Contents

详细描述

下列代码片段演示了如何将一个同步方法的执行方式修改得如异步方式一样。


这里有两个不同的方法:

  • void LongRunningTaskL(TInt& aResult)是一个同步方法,但执行时如异步方式一样,只不过只有当任务完成时才返回;
  • void LongRunningTaskL(MMySynchronousTaskDone* aObserver)是一个同步方法,执行时如异步方法一样。注意只有当它启动并通过MMySynchronousTaskDone来检测任务完成情况时,该方法才会正确返回。


MMP文件

需要下列能力和链接库

CAPABILITY    None
LIBRARY euser.lib


头文件

#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;
};


源文件

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();
}
}


后记

这里的同步方法执行如异步方式一样。

This page was last modified on 15 December 2011, at 09:02.
33 page views in the last 30 days.
×