Use cactiveschedulerwait to convert asynchronous operations to synchronous operations

Source: Internet
Author: User
Recently, I took over the company's mobile game work. Some of them were applications and games developed based on the Symbian platform, so I began to learn related development. Due to the urgency of time, there is no way to study from the first chapter, so we still use the source code + Google method to learn, this part of the content will be scattered, but write down it first.
The following describes the cactiveschedulerwait operations. Article , Reproduced as follows:

I recently read the liuxg IMEI example and finally figured out a little bit about cactiveschedulerwait. Please record it as soon as possible. Otherwise, I will forget it again in a while.

In general, cactiveschedulerwait should be used in an active object to convert an asynchronous operation to a synchronous operation. Take IMEI as an example:
Class cimeiengine: Public cactive
Cimeiengine is an activity object. The core operation to obtain IMEI is an asynchronous operation.Code:
Void cimeiengine: getimei ()
{
If (isactive ())
{
Cancel ();
}

Ctelephony: tphoneidv1pckg phoneidpckg (iphoneid );
Itelephony-> getphoneid (istatus, phoneidpckg );

Setactive ();
Startwait ();
}
We use ctelephony: getphoneid to obtain the IMEI number, but in actual use, it is best to synchronize the cimeiengine: getimei method, which is the most convenient. Imagine the call process:
Iimeiengine-> getimei (); // iimeiengine is a cimeiengine instance
Dosomethingwithimei (); // at this time, the IMEI number must have been obtained successfully.

To ensure that the preceding synchronous call works properly, getimei () must be a synchronous method, which is why cactiveschedulerwait is required.
Let's take a look at the implementation of cimeiengine: startwait:
Void cimeiengine: startwait ()
{
If (iwait. isstarted ()! = (Tint) etrue)
{
Iwait. Start ();
}
}
The code is quite simple, that is, the cactiveschedulerwait: Start method is called.
ProgramAfter you run the startwait () line of code in cimeiengine: getimei, it will be blocked until the cimeiengine: runl is called:
Void cimeiengine: runl ()
{
Iwait. asyncstop ();

If (istatus = kerrnone)
{
// The request is successful
Tbuf Manufacturer = iphoneid. imanufacturer;
Tbuf Model = iphoneid. imodel;
Tbuf Serialnumber = iphoneid. iserialnumber;

If (iobserver)
{
Ctelephony: tphoneidv1pckg phoneidpckg (iphoneid );
Iobserver-> updatephoneinfo (serialnumber );
}
}
}
In runl, first call iwait. asyncstop (), which causes cactiveschedulerwait to end waiting, that is, return
After iwait. Start. However, because of the special nature of the activity object, the iwait. asyncstop () step is not immediately returned to iwait. Start (),
It is returned only after runl is completed, so it is not necessary to put iwait. asyncstop () at the end of runl function.
In this way, cimeiengine: getimei is designed as a synchronization method.

You can also use the user: waitforrequest method to convert the asynchronous mode to synchronous mode. However, it is said that the method cannot work normally in some cases and will be suspended all the time, but I have no chance to encounter this situation.

Digress:
To obtain the IMEI number, you can avoid using cactiveschedulerwait by designing getimei as a synchronization method. This should be executed as early as possible
Ctelephoy: getphoneid, and save the obtained IMEI number. before using the IMEI number, you must determine whether it has been obtained successfully. Either way, none of these solutions
Secure synchronous access.

Note:
In the preceding example, iwait is a member object of cimeiengine:
Class cimeiengine: Public cactive
{
...
PRIVATE:
Cactiveschedulerwait iwait;
...
}
Iwait, As a member variable of cimeiengine, has been constructed after cimeiengine Initialization is complete, so it does not need to be manually constructed or released.

Original address: http://coastline.freepgs.com /? P = 14

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.