Simple use of activity service objects in Symbian

Source: Internet
Author: User
Author: suyouxin

Lead: http://www.symbian.org.cn/bbs/viewforum.php? F = 6

Simple use of activity service objects in Symbian

I have been studying symbain for several months. Today I want to write my own activity service object usage methods.
Symbian officially recommends the use of activity service objects (cactive) instead of multithreading. I think this is a clear truth. In small memory devices such as mobile phones, A program that runs multiple threads consumes a lot of resources. To save resources, Symbian provides an activity service object framework that allows concurrent execution of objects in the Program (not concurrency, in a macro sense, however, the execution is performed in a thread, and the objects of these concurrent jobs are managed through the activity scheduler (activesched.
There are a lot of documents on the Internet about these two things, so I will not talk nonsense here. How can I use them? Here is a simple counter example.
I chose to write an EXE program, that is, the program is based on e32main.

Gldef_c tint e32main ()
{
Ctrapcleanup * cleanup = ctrapcleanup: New ();
Trapd (error, callinstancel ());
If (error! = Kerrnone ){
Printf ("Get error % d/R/N", error );
}
Delete cleanup;
Return 0;
}

The above content is what every EXE file should do. ctrapcleanup * cleanup = ctrapcleanup: New () creates a stack clearing, this allows the program to release all resources in the stack when it exits unexpectedly. of course, you can also add a heap detection Macro. I will not talk about it here. trapd is a commonly used macro in Symbian. Its function is similar to try. The first parameter is to define the name of an error return value variable, followed by a function you write that may be abnormal. when this function is abnormal, the program will not crash, and you can get the cause of the exception. for more information, see the documentation on the Nokia forum.
Next is the vcallinstancel function. In this function, I will create activeschedcel.

Local_c void callinstancel ()
{
Cactivescheduler * scheduler = new (eleave) cactivescheduler ();
Cleanupstack: pushl (schedhl );
Cactivescheduler: Install (Scheduler );
Trapd (error, doinstancel ());
If (error ){
Printf ("error code = % d/R/N", error );
}
Else {
Printf ("OK! /R/n [press any key] ");
}
Cleanupstack: popanddestroy (schedy );
}

This program is easy to create an activity scheduler, press it to clear the stack, and then install the activity scheduler. then execute the real instance function, and finally destroy the output stack. doinstancel we put it to the end to write, and now we will construct our activity counter object.

Class timecount: Public cactive
{
Public:
Static timecount * newlc (); // Constructor
~ Timecount ();
Void startl (); // starts counting
Void constructl ();
Void runl (); // processing function after the delayed event arrives
Void docancel (); // cancel request submission
Void setdelaytime (INT delaytime );
PRIVATE:
Timecount ();
Rtimer itimer; // Timer
Int itimecount; // counter
Int mtime; // count interval in seconds
};

Timecount: timecount ()
: Cactive (0) // you can set the priority of the activity object here.
{
// Add yourself to the activity Planner
Cactivescheduler: add (this );
}

Timecount * timecount: newlc ()
{
Timecount * result = new (eleave) timecount ();
Cleanupstack: pushl (result );
Result-> constructl ();
Return result;
}

Void timecount: docancel (void)
{
Itimer. Cancel ();
}

Void timecount: setdelaytime (INT mtime)
{
Delaytime = mtime;
}

Timecount ::~ Timecount ()
{
Cancel ();
Itimer. Close ();
}

Void timecount: startl ()
{
// Set the timer status to complete every mtime second
Itimer. After (istatus, 10000*100 * mtime );
// Submit an asynchronous request
Setactive ();
}

Void timecount: constructl ()
{
// Initialize the counter and Timer
Itimecount = 0;
User: leaveiferror (itimer. createlocal ());
}

Void timecount: runl ()
{
// Continue to submit delayed request events after the counter + 1
Printf ("the count is-> % d", itimecount ++ );
Startl ();
}

Each active service object has an istatus to identify the status of the current object. here we set istatus to itimer. after (istatus, 10000*100 * mtime); that is, the istatus changes after the timer mtime second. At this time, the activity scheduler receives a change in the status, so as to call the processing function of the corresponding activity object, that is, the runl function. count and output in the runl function, call startl to reset the timer and object status, and submit it to the activity scheduler. in this way, the scheduler will call the runl function again after the mtime second. the counter is always repeated in this way.

Finally, write the doinstancel function.
Local_c void doinstancel ()
{
Timecount * timecount = timecount: newlc ();
// Print every second
Timecount-> setdelaytime (1 );
Timecount-> startl ();

Cactivescheduler: Start ();

Cleanupstack: popanddestroy (1 );
}
After the object is created, the cactivescheduler: Start () program starts to run. This statement tells the event scheduler to wait for the object to change its status, here is the change of istatus of timecount. after istatus changes and runl is called, we can continue to wait for the changes of iststus so that we can use the active object counter to run through the message driver.
Here, cactivesched only manages one cactive object, that is, timecount. You can use a similar method to implement multiple cactive objects and add them to cactivescheduler, cactivescheduler will wait for changes in the status of all the cactive instances that are added to it. One of the changes will execute the corresponding processing function of the active object. When the status changes at the same time, the object priority is used to determine the runl function that is called first. cactivescheduler is also non-preemptible. When a runl function has not been executed, if the status of another cactive function changes, it will wait until runl is executed and then execute another cactive function.
I want to write it today. I will try again later.

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.