// File:HelloWorld.cpp
// Description:This program just a example for multitasking to tell how to use the active object.
// 其实我是一个初学者,由于感觉这个部分比较难理解所以经过琢磨写出了这个程序.其实
// 这个程序是模仿Symbian的例子程序写的,也算使我对活动对象实现多任务程序的理解
// 希望能够帮助学习symbian的广大初学者朋友们.
#include "CommonFramework.h"
_LIT(KExampleTimerPanic, "CExampleTimer");
class CExampleTimer : public CActive
{
public:
static CExampleTimer* NewLC();
~CExampleTimer();
void After(TTimeIntervalMicroSeconds32& aInterval);
protected:
CExampleTimer();
void ConstructL();
protected:
virtual void RunL(); // Inherited from CActive
virtual void DoCancel();
virtual TInt RunError(TInt aError);
private:
RTimer iTimer;
TTimeIntervalMicroSeconds32 iInterval;
TInt iNo; //对象的标识符。
static TInt iNum;//用来保存当前的对象是第几个.在创建对象的时候把它的值赋给iNo.
};
TInt CExampleTimer::iNum = 0;
CExampleTimer::CExampleTimer() : CActive(EPriorityStandard)
{
iNo = ++iNum;//对iNo赋值。
CActiveScheduler::Add(this);
}
void CExampleTimer::ConstructL()//二步构造
{
// Create the asynchronous service provider
User::LeaveIfError(iTimer.CreateLocal()); //打开iTimer
}
CExampleTimer* CExampleTimer::NewLC()
{
CExampleTimer* self = new( ELeave ) CExampleTimer;
CleanupStack::PushL( self );
self->ConstructL();
return self;
}
CExampleTimer::~CExampleTimer()
{
Cancel(); //Cancel()调用DoCancel()
iTimer.Close();//关闭iTimer
}
void CExampleTimer::After(TTimeIntervalMicroSeconds32& aInterval)//用来发出请求.
{
// Only allow one timer request to be submitted at a time
// Caller must call Cancel() before submitting another
__ASSERT_ALWAYS(!IsActive(), User::Panic(KExampleTimerPanic, KErrInUse));
iInterval = aInterval;
iTimer.After(iStatus,aInterval); // Set the RTimer
//当时间到的时候SetActive(),使对象自己的优先级变高成为活跃对象,调度程序就执行它.
SetActive();
}
void CExampleTimer::RunL()//调度程序调用此函数.来执行事件处理操作.
{
// If an error occurred (admittedly unlikely) deal with the problem in RunError()
User::LeaveIfError(iStatus.Int());
// Otherwise, log the timer completion and resubmit the timer
_LIT(KTimerExpired, "Timer Expired : %d\n");//打印对象标志信息
console->Printf(KTimerExpired,iNo); //
_LIT(KTime,"Delay: %d\n"); //
console->Printf(KTime,iInterval); //
iTimer.After(iStatus, iInterval);//再次发出请求已期待下次执行.
SetActive();//当时间到的时候SetActive(),使对象自己的优先级变高成为活跃对象,调度程序就执行它.
}
void CExampleTimer::DoCancel()
{
// Cancel the timer
iTimer.Cancel();
}
TInt CExampleTimer::RunError(TInt aError)
{
// Called if RunL() leaves, aError contains the leave code
_LIT(KErrorLog, "Timer error %d");
console->Printf(KErrorLog, aError); // Logs the error
return (KErrNone); // Error has been handled
}
// do the example
LOCAL_C void doExampleL()
{
// Construct and install the active scheduler
CActiveScheduler* scheduler = new (ELeave) CActiveScheduler;
CleanupStack::PushL( scheduler );
// Install as the active scheduler
CActiveScheduler::Install( scheduler );
// Create an active object
CExampleTimer* timer = CExampleTimer::NewLC();
CExampleTimer* timer2 = CExampleTimer::NewLC();
TTimeIntervalMicroSeconds32 interval(5000000);//设置两个时间长度使为了更直观的显示多任务
TTimeIntervalMicroSeconds32 interval2(10000000);//
// Issue the first request
timer->After( interval );//第一个对象每5秒钟执行一次runL.
timer2->After( interval2 );//第二个对象每10秒执行一次runL
// CExampleTimer* timer1 = CExampleTimer::NewLC();
// TTimeIntervalMicroSeconds32 interval1(2000000 );
// Issue the first request
// timer1->After( interval1 );
// Main part of program is a wait loop
// This function completes when the scheduler stops
CActiveScheduler::Start();//调度程序开始执行。
// Remove from the cleanup stack and destroy:
// 1. the CWriteKeyProcessor active object
// 2. exampleScheduler
CleanupStack::PopAndDestroy(2);
}