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++;
}