|
||
It is possible to maintain an outstanding request from the active
scheduler, by overriding the CActiveScheduler::WaitForAnyRequest()
function.
In this case, it is useful for the active scheduler to have a data member which points to the active object for which it will maintain an outstanding request. Implement the active scheduler as follows:
class CExampleScheduler : public CActiveScheduler
{
public:
void Error (TInt aError) const;
void WaitForAnyRequest();
void SetActiveObject(CActiveConsole* aActiveConsole);
private:
CActiveConsole* iActiveConsole;
};
void CExampleScheduler::SetActiveObject(CActiveConsole* aActiveConsole)
{
iActiveConsole = aActiveConsole;
}
where iActiveConsole
is a pointer to an active object,
initialised by call to SetActiveObject()
during the construction
of the controlling active object:
void CMessageKeyProcessor::ConstructL()
{
CActiveScheduler::Add(this);
(CExampleScheduler*)(CActiveScheduler::Current())->SetActiveObject(this);
}
Now override
CActiveScheduler::WaitForAnyRequest()
:
void CExampleScheduler::WaitForAnyRequest()
{
if (!(iActiveConsole->IsActive()))
{
iActiveConsole->RequestCharacter();
}
CActiveScheduler::WaitForAnyRequest();
}
The state of the active request flag is examined and a request is not issued if it is active. This check is important because the function may be called as a result of other active objects completing (for example, a timed messenger) and in these cases it would be inappropriate to renew the keyboard service request.
It is no longer necessary to call
RequestCharacter()
either before starting the active scheduler, or
as part of key processing.
The static function CActiveScheduler::Current()
is
used to fetch a pointer to the currently installed active scheduler (in this
example, an object of type CExampleScheduler
). This is necessary
in order to access the SetActiveObject()
member function of
CExampleScheduler
.