📄 integersummationthread.cpp
字号:
/**
*
* Definition of CIntegerSummationThread
*
* Copyright (c) 2004 Nokia Corporation
* version 2.0
*/
// INCLUDE FILES
// User includes
#include "IntegerSummationThread.h"
#include "PowerResMan.hrh"
_LIT(KString,"SummationThread"); // name of thread
_LIT(KPRM,"PowerResMan"); // for panic
const TInt KMaxStackSize = 12000;
//////////////////////////////////////////////////////////////////////
// Global Thread functions
//////////////////////////////////////////////////////////////////////
static TInt ThreadMain(TAny* aParam);
static TInt ThreadFunctionL(TAny* aParam);
// A pointer to this function will be passed to the thread on creation
// Control will pass to this function when the thread is first resumed,
// i.e. when the thread is initially scheduled to run
TInt ThreadMain(TAny* aParam)
{
// the threadmain function this called by the engine to kick off the thread
CTrapCleanup* cleanupStack=CTrapCleanup::New();
// the
TRAPD(error,ThreadFunctionL(aParam));
// we do not panic this thread with the leave but pass an error
// code back to the Active object which itself can leave.
delete cleanupStack;
return error;
}
// second stage startup for the server thread
TInt ThreadFunctionL(TAny* aParam)
{
// the thread function that calls the engine summation method.
CIntegerSummationThread* engine = (CIntegerSummationThread*)aParam;
// call the method on the engine
engine->DoSum();
return 0;
}
// Creates instance of CIntegerSummationThread
CIntegerSummationThread* CIntegerSummationThread::NewL(MSummationObserver& aObserver)
{
CIntegerSummationThread* self = new (ELeave) CIntegerSummationThread(aObserver);
CleanupStack::PushL(self);
self->ConstructL();
CleanupStack::Pop(self);
return self;
}
// Constructor
CIntegerSummationThread::CIntegerSummationThread(MSummationObserver& aObserver)
: CActive(EPriorityStandard), iObserver(aObserver)
{
}
// Destructor
CIntegerSummationThread::~CIntegerSummationThread()
{
// Make sure this active object is cancelled
Cancel();
}
// 2nd Phase Constructor
void CIntegerSummationThread::ConstructL()
{
// Add this active object to the active scheduler
CActiveScheduler::Add(this);
}
// This function is called to initiate the calculation
void CIntegerSummationThread::StartSum()
{
// cleanup before we start
iThread.Close();
// create the thread and set this object
// to be notified on the death of the thread(completion of task).
TAny* engineAsParam = this;
TInt error=iThread.Create(KString,ThreadMain,KMaxStackSize,NULL,engineAsParam);
// Panic if thread creation fails.
if (error != KErrNone)
{
Panic(EUnableToCreateThread);
}
else
{
// Request notification of when the thread dies, normally or otherwise
iThread.Logon(iStatus);
// set a low priority for the thread
iThread.SetPriority(EPriorityLess);
// Indicatte a request is outstanding
SetActive();
// Make the thread eligible for execution
iThread.Resume();
}
}
// Carries out the whole calculation
void CIntegerSummationThread::DoSum()
{
// Do the calculation
iTotal =0;
for (TInt i = 0; i < (EFinalPhase+1)*100; i++)
iTotal += i;
}
// This will be called once the thread has completed
void CIntegerSummationThread::RunL()
{
//cleanup
iThread.Close();
// if the thread has left we will leave
if (iStatus != KErrNone)
{
User::Leave(iStatus.Int());
}
// Notify the observer that we have finished
Completed();
}
// Cancel outstanding request - called by Cancel()
void CIntegerSummationThread::DoCancel()
{
}
// Called when the task is complete
// Notifies the observer of the result
void CIntegerSummationThread::Completed()
{
// Teel the observer we have finished - and supply the total
iObserver.SummationComplete(iTotal);
}
// Panics the application
void CIntegerSummationThread::Panic(TPanicCode aReason) const
{
User::Panic(KPRM,aReason);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -