Some simple uses of active objects in Symbian

xiaoxiao2021-03-06  39

Some simple uses of active service objects in Symbian

The study of Symbain has been a few months. Today, I will write some of my active service objects. Symbian officially recommends using the active service object (CACTIVE) instead of multi-threaded use, I think this truth is very clear. In small memory devices such as mobile phones, run multithreaded programs are very resource-consuming. In order to save resources, Symbian provides a framework of an active service object, allowing the program to execute the object (it is not concurrent, but macro looks Come to be in one thread, these concurrent works are managed by Activescheduler. About these two things, there is a lot of documents online, I am not talking about it, how to Use? Here I will give an example of a simple counter. I chose to write an exe program, that is, the program is entry with 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 should be done every EXE file, ctrapcleanup * cleanup = ctrapcleanup :: new () creates a clear stack so that the program releases the resources in the stack when the program is exited. Of course you can also add The upper pile detection macro, here I don't say .Trapd is the macro that is often used in Symbian. The function is similar to TRY. The first parameter is the name of the definition to return value variables, and there may be abnormal you write. Function. When this function is abnormal, the program does not Crash, you can get an exception. You can refer to some documents on the NOKIA forum. Next is the vcallinstancel function, in this function I will build Activescheduler.

LOCAL_C void callInstanceL () {CActiveScheduler * scheduler = new (ELeave) CActiveScheduler (); CleanupStack :: PushL (scheduler); 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 (Scheduler);}

This program is very simple to create an event planner and press it to clear the stack, then install the active planner, so you can use the real instance function, and finally the stack is destroyed. DOINSTANCEL We put it in the final way, Now construct our active counter object.

Class timecount: public cactive {public: static timecount * newlc (); // Constructor ~ timecount (); void startl (); // count Start Void ConstructL (); void Runl (); // After the delay event reaches later Process function void Docancel (); // Cancel request Submit Void setDelaytime; private: timecount (); rtimer itimer; // timer int itimecount; // counter int mtime; // count interval time unit second} Timecount :: timecount (): CACTIVE (0) // This can set the priority of the active object {// to add yourself to the active planner CACTIVESCHEDULER :: Add (this);

Timecount * timecount :: newlc () {timecount * result = new (elevent) 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 () {// Setting the timer status to complete the ITIMER.AFTER (ISTATUS, 1000 * 100 * Mtime) every MTIME second state; // submit asynchronous request setActive ();

Void Timecount :: ConstructL () {// Initialization Counter and Timer ItimeCount = 0; user :: leaveiferror (itimer.createlocal ());}

Void timecount :: run () {// counter 1 Continue to submit the delay request event Printf ("THE COUNT IS - >>% D", ITimeCount ); startl ();

转载请注明原文地址:https://www.9cbs.com/read-72248.html

New Post(0)