Symbian Programming Summary-Basic article-active object Positive solution (4)-synchronous invocation of asynchronous function

Source: Internet
Author: User
Tags thread

In the last section of our in-depth understanding of the active object, the active scheduler and asynchronous function server working principle and operating mechanism, we must be the activities of the object of the mechanism and the application of the system has been well known. But do you think asynchronous functions are more troublesome to use? Is it not possible to invoke an asynchronous function in an "asynchronous" way instead of using the active object? This section will solve this problem for you: synchronous use of asynchronous functions.

First, the use of cactiveschedulerwait class

In the previous article "Symbian Programming Summary-Interface section-Open jpeg/gif/png image" We have seen the use of the Cactiveschedulerwait class, I will explain in detail.

Many beginners will mix Cactivescheduler and cactiveschedulerwait classes at the beginning, Cactivescheduler is the scheduler of the active object. And cactiveschedulerwait can be simply understood as a blocking device for the current thread:

When the Cactiveschedulerwait::start () method is invoked, the thread blocks;

When calling the Cactiveschedulerwait::asyncstop () method, the request stops blocking the thread

So, without modifying the original active object code, simply add "Cactiveschedulerwait::start ()" After the asynchronous function calls the method and add "Cactiveschedulerwait" at the beginning of the RUNL method of the active object: : Ansycstop () "on it."

For the console application described in the previous tutorial, we modified the following methods (underlined for the modified section):

cactiveschedulerwait* iwait;

void Cmyactiveobject::constructl ()
{
User::leaveiferror (Itimer.createlocal ()); Initialize Timer
Cactivescheduler::add (this); ADD to Scheduler

iwait = new (Eleave) cactiveschedulerwait;
}

Cmyactiveobject::~cmyactiveobject ()
{
Cancel (); Cancel any request, if outstanding
Itimer.close (); Destroy the Rtimer Object
Delete instance variables if any

if (iwait->isstarted ())
{
Iwait->asyncstop ();
}

Delete iwait;
iwait = NULL;
}

void Cmyactiveobject::startl (TTimeIntervalMicroSeconds32 adelay)
{
Cancel (); Cancel any request, just to be sure

Itimer.after (Istatus, Adelay); Set for later
SetActive (); Tell Scheduler a request is active

Iwait->start (); 1th
}

void Cmyactiveobject::runl ()
{
Iwait->asyncstop (); 2nd

Tbuf<50> Outputstr;
Outputstr.appendnum (icount);
Iconsole.write (OUTPUTSTR);
Iconsole.write (_l ("\ n"));
icount++;
}

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.