?? activeobject.cpp
字號:
#include "doMain.h"
/*
這里是Active Object的一個(gè)Demo,在這個(gè)Demo中 構(gòu)建兩個(gè)活動對象
*/
//====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;
};
//調(diào)用基類構(gòu)造當(dāng)前活動對象
//參數(shù)可以設(shè)置優(yōu)先級
TmCount::TmCount():CActive(0) // 這里可以設(shè)置活動對象的優(yōu)先級
{
// 把自己加入活動規(guī)劃器
iTimeCounter=1;
CActiveScheduler::Add(this);
};
TmCount* TmCount::NewLC()
{
console->Printf(_L("NewLC\n"));
// 使用 C++默認(rèn)構(gòu)造函數(shù)構(gòu)造
TmCount* result = new (ELeave) TmCount();
// 把活動對象推入清理粘
CleanupStack::PushL( result );
// 調(diào)用兩階段構(gòu)造函數(shù),構(gòu)造活動對象內(nèi)部成員
result->ConstructL();
// 返回活動對象指針
return result;
};
void TmCount::DoCancel()
{
iTimer.Cancel();
};
void TmCount::SetDelayTime(TInt delaytime)
{
DelayTime = delaytime;
};
void TmCount::ConstructL()
{
// 初始化計(jì)數(shù)器和定時(shí)器
iTimeCounter = 0;
User::LeaveIfError(iTimer.CreateLocal());
console->Printf(_L("ConstructL\n"));
};
// 活動對象的請求函數(shù)
void TmCount::StartL(TInt mT)
{
// 將請求傳遞給異步服務(wù)提供者,
// 異步服務(wù)提供者,調(diào)用請求函數(shù),
// 這里需要注意iStatus 這個(gè)參數(shù),它是一個(gè)請求狀態(tài)對象。
// 設(shè)定定時(shí)器狀態(tài)為每隔mTime秒鐘狀態(tài)完成一次
iTimer.After(iStatus, 10000 * 100 * mT);
// 在調(diào)用異步請求函數(shù)之后調(diào)用SteActive()方法,提交異步請求
SetActive();
};
void TmCount::RunL()
{
// 計(jì)數(shù)器+1以后繼續(xù)提交延時(shí)請求事件
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;
};
//調(diào)用基類構(gòu)造當(dāng)前活動對象
//參數(shù)可以設(shè)置優(yōu)先級
TmCountTwo::TmCountTwo():CActive(0) // 這里可以設(shè)置活動對象的優(yōu)先級
{
// 把自己加入活動規(guī)劃器
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()
{
// 初始化計(jì)數(shù)器和定時(shí)器
iTimeCounter = 0;
User::LeaveIfError(iTimer.CreateLocal());
console->Printf(_L("ConstructL\n"));
};
void TmCountTwo::StartL(TInt mT)
{
// 設(shè)定定時(shí)器狀態(tài)為每隔mTime秒鐘狀態(tài)完成一次
iTimer.After(iStatus, 10000 * 50 * mT);
// 提交異步請求
SetActive();
};
void TmCountTwo::RunL()
{
// 計(jì)數(shù)器+1以后繼續(xù)提交延時(shí)請求事件
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 活動對象安裝為當(dāng)前線程的活動對象
// 安裝以后,sco 活動對象專門處理當(dāng)前線程的請求事件
CActiveScheduler::Install(sco);
// 卸載當(dāng)前線程的活動對象
//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);
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -