Symbian programming Summary-basics-positive solution of activity objects (3)-working principle of activity objects

Source: Internet
Author: User

This article was originally prepared by Yang. If you need to reselect the article, please indicate the source. Thank you!

In the previous section, we have learned how to create and use activity objects. You have a certain understanding of the creation and use of activity objects. In this section, I will go deep into the activity object mechanism, which is divided into two parts: "activity object Workflow" and "signal loss error", to analyze the working principle of the activity object.

I. workflow of activity targets

First, we use the sequence diagram to illustrate the process of creating and calling between applications, activity objects, activity schedulers, and asynchronous function servers:

The following describes how to use the code for each step (Click here to download the code:

1. Create and install the active scheduler:

     CActiveScheduler* scheduler = new (ELeave) CActiveScheduler();    CleanupStack::PushL(scheduler);    CActiveScheduler::Install(scheduler);

If you have created an application based on the GUI application framework and the framework has already created and installed an active scheduler for us, we can directly use a series of methods of CActiveScheduler.

2. Create activity objects

 iMyAO = CMyActiveObject::NewL(*console);

The CMyActiveObject class created here is the activity object inherited from the CActive class.

3. Add the activity object to the activity scheduler.

 void CMyActiveObject::ConstructL()    {    ....    CActiveScheduler::Add( this); // Add to scheduler    }

As you can see, when an activity object is created through the "two-phase structure", its pointer has been added to the activity scheduler.

4. StartL

StartL calls the asynchronous Function Method for the activity object requested by the application. You can rename this method as needed:

 Void CMyActiveObject: StartL (TTimeIntervalMicroSeconds32 aDelay) {Cancel (); // Cancel the asynchronous function request iStatus = KRequestPending; iTimer. after (iStatus, aDelay); // call the asynchronous function SetActive () here; // set the member variable iActive = ETrue}

The StartL method cannot be called again when the asynchronous function call is completed. Therefore, the Cancel () method is called to Cancel the asynchronous request at the entry point of the StartL method, otherwise, the "Lost signal" error may occur.

5. iStatus = KRequestPending

In the StartL method of the above Code, before calling an asynchronous function, you must first set iStatus to KRequestPending to enable the active scheduler to perform time-over-time matching.

6. Request asynchronous functions and send iStatus

In the StartL method, iTimer. After (iStatus, aDelay); this line of code passesReferenceThe asynchronous function iTimer. After is called.

7. SetActive

Call the SetActive method of the base class CActive to set the iActive to ETrue in the method so that the active scheduler can match the time and duration.

8. Start the activity Scheduler

 CActiveScheduler::Start();

10. Search for the corresponding activity object

We have analyzed and restored the pseudo code of the CActiveScheduler: Start () method in the previous section. This code block traverses all the activity objects registered with the scheduler in another thread, check that the object's iStatus is not KRequestPending and the iActive is ETrue. After the asynchronous function server completes the request, it will change the real parameter of iStatus so that it is not equal to KRequestPending, in addition, the iActive value of the activity object is changed to ETrue immediately after the asynchronous function is returned, so the activity Scheduler only needs to judge iStatus! = KRequestPending & iActive = ETrue, you can know which activity object requested the asynchronous service has been completed.

9. WaitForAnyRequest () waits for the asynchronous function to return a notification. 11. RequestComplete () and changes the value of iStatus.

When the asynchronous service has completed the requested work, it will use User: RequestComplete () to send a notification, the activity scheduler will use User: WaitForAnyRequest () or RThread :: waitForAnyRequest () receives this notification, and then traverses the activity object that matches the conditions it is registered with (8th underlines ).

12. Call the RunL Method

If the activity object corresponding to the asynchronous function is found, the RunL method of the activity object is called, and the RunL method runs in the TRAP macro. If the RunL method throws an exception, the activity scheduler automatically calls the RunError method of the activity object. Therefore, you do not need to write exception capture code in RunL in most cases.

After the RunL method is called for an activity object, set the iActive value of the activity object to EFalse so that the processed activity object can be skipped the next time the collection is traversed.

If the activity object corresponding to the asynchronous function is not found, the activity scheduler throws a "lost signal" exception.

13. CActiveScheduler: Stop ()

Stop the event scheduler and stop monitoring signals returned by asynchronous functions.

Ii. Dedicated error of activity objects-"Lost signal"

From the above analysis, we know that the activity scheduler intercepts the messages returned by the asynchronous function, and then traverses the activity object set to find the corresponding activity object call. If not, it throws a "signal loss" exception. You can use either of the following methods to "cannot find the activity object:

  1. The activity object is not registered to the activity scheduler at all: the CActiveScheduler: Add method is not called to register the activity object.
  2. Unable to meet the iStatus standard used by the activity scheduler to search for activity objects! = KRequestPending & iActive = ETrue: After the asynchronous function is called, The SetActive () method is not called to set the value of iActive to ETrue; or the iStatus is sent to two asynchronous functions at the same time, when the second asynchronous function returns, the iActive value of the current active object is EFalse, And the Scheduler cannot find the corresponding activity object.

Iii. Summary

In this section, we learned the internal working principle of the activity object and gained a deeper understanding of the activity object processing mechanism. In the next section, we will learn how to synchronously call existing asynchronous functions.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.