In the previous section we have a general understanding of the basic concept of active objects, to use the active object mechanism, we need to use the active object, active scheduler, asynchronous functions. We want to use asynchronous functions, which are used in accordance with the process of the application-> activity-> the active scheduler-> asynchronous functions. Next we start to go into combat and use the active object.
First, create an activity scheduler
We know that the active scheduler is a bridge between application and asynchronous functions that use active objects to intercept the return "completion" message of an asynchronous function through the activity scheduler and notify the application in the manner of events.
Using carbide C + + 1.3, the console program generated by the Template Wizard automatically generates code for us to create the active scheduler:
CActiveScheduler* scheduler = new (ELeave) CActiveScheduler();
CActiveScheduler::Install(scheduler);
After the Cactivescheduler::install () method is called, the internal code assigns the scheduler pointer to a static pointer inside the Cactivescheduler class. The following code makes it easy to use static methods of the Cactivescheduler class, such as:
IMPORT_C static void Add(CActive* aActive);
IMPORT_C static void Start();
IMPORT_C static void Stop();
Add () Method: Add the active object to the active scheduler to register for use
Start () Method: Start the Activity Scheduler, the activity Scheduler will begin to wait for the notification message returned by the asynchronous function
Stop () Method: Stops the active scheduler
Second, create the active object
1. The active object we create must derive from the Cactive class, and the Cactive class has prepared the Istatus member variable for us:
public:
TRequestStatus iStatus;
private:
TBool iActive;
Another member variable, iactive, acts as an identifier, proving that the active object has requested an asynchronous function, such as:
RTimer::After(iStatus, 1000000);
SetActive();
The SetActive () method is a cactive method of the base class, in fact, the iactive = Etrue is used to identify the active object that has already called the asynchronous function. So, as long as we call the asynchronous function, we should call the code of the SetActive () method next to the code that calls the asynchronous function.
2, there are two virtual methods must inherit:
virtual void DoCancel() =0;
virtual void RunL() =0;
Runl method: The active scheduler receives the completed message returned by an asynchronous function, iterates through all the active objects it registers, and invokes the!= method of the active object if the active object's iactive = Etrue and Istatus krequestpending runl. The iactive is set to Efalse to prevent this active object from being invoked the next time polling.
Here the name "RunL" will make many people ambiguous, when I first started to contact with the J2ME in the Runnable interface of the Run method is similar. In fact, it is more appropriate to change "RunL" to "Notifyrequestcompletel" here.
Again, when calling an asynchronous function, the parameter trequeststatus& status is passed in a reference way, such as:
IMPORT_C void After(TRequestStatus &aStatus, TTimeIntervalMicroSeconds32 anInterval);
Therefore, an asynchronous function can change the actual parameter of the status, that is, the class member istatus of the active object.
Docancel () Method: There is a method of canceling an asynchronous function in the base class Cactive cancel (), after calling Cancel (), the active object notifies the application by the Docancel () method of the late work of the cancellation method, such as deleting the object and retrieving the pointer. Note: If you want to terminate the active object in your application, use the Cancel () method call instead of the Docancel () method.