Friday, April 3, 2009

Usefull CIdle in RunL() of a CActive derived class.

The main purpose for which I am using instances of CIdle is in the RunL() method of a CActive derived class.

Everywhere you read that a RunL() method has to complete as fast as possible for not blocking incoming events for other CActive derived instances. But how can this be achieved?

The solution is to "wind up" a dispatcher in the RunL() method.
For this the CActive derived class has to aggregate a member of type CIdle*.
In the body of RunL() the following code is implemented.

void CMyCActiveDerived::RunL()
{
/* ... some handle code ... */

if (!iDispatcher) iDispatcher = CIdle::NewL(CActive::EPriorityNormal);
else iDispatcher->Cancel();
iDispatcher->Start(TCallBack(EventHandlerL, this));

/* ... some optional code ... */
}

The CActive derived class has to provide a static EventHandlerL method. Optional you can use any static member function from any other class. The second argument of the temporary created TCallBack instance is a pointer of any type (a void* ;-))

The advantage: The current stack frame is destroyed and the event handler code is called in a delayed way. That's decoupling.

Any comments appreciated.