📄 activeobject.cpp
字号:
#include "doMain.h"
/*
这里是Active Object的一个Demo,在这个Demo中 构建两个活动对象
*/
//====Class TmCount===================================
class TmCount:public CActive
{
public:
static TmCount* NewLC();
~TmCount();
void ConstructL();
void RunL();
TInt RunError(TInt aError);
void DoCancel();
void SetDelayTime(TInt delaytime);
void StartL(TInt mT);
private:
TmCount();
RTimer iTimer;
TInt iTimeCounter;
TInt mTime;
TInt DelayTime;
};
//调用基类构造当前活动对象
//参数可以设置优先级
TmCount::TmCount():CActive(0) // 这里可以设置活动对象的优先级
{
// 把自己加入活动规划器
iTimeCounter=1;
CActiveScheduler::Add(this);
};
TmCount* TmCount::NewLC()
{
console->Printf(_L("NewLC\n"));
// 使用 C++默认构造函数构造
TmCount* result = new (ELeave) TmCount();
// 把活动对象推入清理粘
CleanupStack::PushL( result );
// 调用两阶段构造函数,构造活动对象内部成员
result->ConstructL();
// 返回活动对象指针
return result;
};
void TmCount::DoCancel()
{
iTimer.Cancel();
};
void TmCount::SetDelayTime(TInt delaytime)
{
DelayTime = delaytime;
};
void TmCount::ConstructL()
{
// 初始化计数器和定时器
iTimeCounter = 0;
User::LeaveIfError(iTimer.CreateLocal());
console->Printf(_L("ConstructL\n"));
};
// 活动对象的请求函数
void TmCount::StartL(TInt mT)
{
// 将请求传递给异步服务提供者,
// 异步服务提供者,调用请求函数,
// 这里需要注意iStatus 这个参数,它是一个请求状态对象。
// 设定定时器状态为每隔mTime秒钟状态完成一次
iTimer.After(iStatus, 10000 * 100 * mT);
// 在调用异步请求函数之后调用SteActive()方法,提交异步请求
SetActive();
};
void TmCount::RunL()
{
// 计数器+1以后继续提交延时请求事件
console->Printf(_L("[1]The Count is ->>%d\n"), iTimeCounter++);
if (iTimeCounter<10)
{
StartL(1);
}
else
{
Cancel();
}
//User::Leave(9);
};
TInt TmCount::RunError(TInt aError)
{
console->Printf(_L("[1]Error\n"));
//console->Getch();
return 7;
};
TmCount::~TmCount()
{};
//======TmCountTwo===============================================
class TmCountTwo:public CActive
{
public:
static TmCountTwo* NewLC();
~TmCountTwo();
void ConstructL();
void RunL();
void DoCancel();
void SetDelayTime(TInt delaytime);
void StartL(TInt mT);
private:
TmCountTwo();
RTimer iTimer;
TInt iTimeCounter;
TInt mTime;
TInt DelayTime;
};
//调用基类构造当前活动对象
//参数可以设置优先级
TmCountTwo::TmCountTwo():CActive(0) // 这里可以设置活动对象的优先级
{
// 把自己加入活动规划器
CActiveScheduler::Add(this);
};
TmCountTwo* TmCountTwo::NewLC()
{
console->Printf(_L("NewLC\n"));
TmCountTwo* result = new (ELeave) TmCountTwo();
CleanupStack::PushL( result );
result->ConstructL();
return result;
};
void TmCountTwo::DoCancel()
{
iTimer.Cancel();
};
void TmCountTwo::SetDelayTime(TInt delaytime)
{
DelayTime = delaytime;
};
void TmCountTwo::ConstructL()
{
// 初始化计数器和定时器
iTimeCounter = 0;
User::LeaveIfError(iTimer.CreateLocal());
console->Printf(_L("ConstructL\n"));
};
void TmCountTwo::StartL(TInt mT)
{
// 设定定时器状态为每隔mTime秒钟状态完成一次
iTimer.After(iStatus, 10000 * 50 * mT);
// 提交异步请求
SetActive();
};
void TmCountTwo::RunL()
{
// 计数器+1以后继续提交延时请求事件
console->Printf(_L("[2]The Count is ->>%d\n"), iTimeCounter++);
StartL(1);
};
TmCountTwo::~TmCountTwo()
{};
//=====================================
class CATV :public CActiveScheduler
{
public :
IMPORT_C void Error(TInt anError) const;
};
void CATV::Error(TInt anError) const
{
console->Printf(_L("Error:%d\n"),anError);
//CATV::Stop();
};
//===ActiveL()==============================================================
LOCAL_C void ActiveL()
{
//CActiveScheduler* sco = new(ELeave) CActiveScheduler();
CATV* sco = new(ELeave) CATV();
CleanupStack::PushL(sco);
// 把 sco 活动对象安装为当前线程的活动对象
// 安装以后,sco 活动对象专门处理当前线程的请求事件
CActiveScheduler::Install(sco);
// 卸载当前线程的活动对象
//CActiveScheduler::Install(NULL);
CActiveScheduler* sco2 =CActiveScheduler::Current();
TmCount* timeCount = TmCount::NewLC();
// 每隔一秒钟打印一次
timeCount->SetDelayTime(1);
timeCount->StartL(1);
TmCountTwo* timeCount2 = TmCountTwo::NewLC();
// 每隔一秒钟打印一次
timeCount2->SetDelayTime(1);
timeCount2->StartL(1);
//console->Getch();
CActiveScheduler::Start();
CleanupStack::PopAndDestroy(2);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -