This page was last modified 11:49, 20 March 2008.
Running an Active Object in OpenC
From Forum Nokia Wiki
I just want to use some functions and classes of Symbian C++ in an Open C project. Because some classes are written as Active Object for asynchronisation. I am trying to find out, if it is possible to run an active class in a Open C program. And finally it works. At least on device, not yet on emulator by compiling.
Here is the code, may be someone needs it. I am using the example 'Periodic' from the SDK as the CActive class.
main.c
#include <stdio.h> #include <e32base.h> #ifdef __GCCE__ #include <staticlibinit_gcce.h> #endif #include "PeriodicTest.h" _LIT(KTxtEPOC32EX,"EPOC32EX"); void activeClassL() { CActiveScheduler* scheduler = new (ELeave) CActiveScheduler(); CleanupStack::PushL(scheduler); CActiveScheduler::Install(scheduler); CPeriodicRunner* periodic = CPeriodicRunner::NewL(4); periodic->Start(); CActiveScheduler::Start(); periodic = NULL; // Delete active scheduler CleanupStack::PopAndDestroy(scheduler); } void runCActiveclass() { __UHEAP_MARK; CTrapCleanup* cleanup = CTrapCleanup::New(); TRAPD(error,activeClassL()); // more initialization, then do example __ASSERT_ALWAYS(!error,User::Panic(KTxtEPOC32EX,error)); delete cleanup; __UHEAP_MARKEND; } int main() { runCActiveclass(); printf("finished!\n"); getchar(); return 0; }
PeriodicTest.h
#ifndef PERIODICTEST_H_ #define PERIODICTEST_H_ #include <e32base.h> #include <stdio.h> class CPeriodicRunner : public CActive { public: static CPeriodicRunner* NewL(TInt aTotalTicks); ~CPeriodicRunner(); // destruct and give statistics protected: CPeriodicRunner(TInt aTotalTicks); private: void ConstructL(); // second construction phase void RunL(); void DoCancel(); void DoTick(); // indirectly called public: void Start(); private: TInt iTotalTicks; RTimer timer; }; #endif /*PERIODICTEST_H_*/
PeriodicTest.cpp
#include "PeriodicTest.h" // protected C++ constructor CPeriodicRunner::CPeriodicRunner(TInt aTotalTicks) : CActive(EPriorityStandard), iTotalTicks(aTotalTicks) { } // private second-phase constructor void CPeriodicRunner::ConstructL() { timer.CreateLocal(); CActiveScheduler::Add( this ); } // construct, add CPeriodic to active scheduler, and start it CPeriodicRunner* CPeriodicRunner::NewL(TInt aTotalTicks) { CPeriodicRunner* self=new (ELeave) CPeriodicRunner(aTotalTicks); CleanupStack::PushL(self); self->ConstructL(); CleanupStack::Pop(); return self; } // destruct and give statistics CPeriodicRunner::~CPeriodicRunner() { timer.Close(); CActiveScheduler::Stop(); } void CPeriodicRunner::RunL() { if (iStatus == KErrNone) { DoTick(); } } void CPeriodicRunner::DoCancel() { } void CPeriodicRunner::Start() { timer.After(iStatus, 1000000); SetActive(); } // private void CPeriodicRunner::DoTick() { iTotalTicks--; printf("Periodic timer %d done\n", iTotalTicks); if(iTotalTicks==0) { delete this; } else { timer.After(iStatus, 1000000); SetActive(); } }
--Paipeng 13:49, 20 March 2008 (EET)
| Related Discussions | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to use Active object in thread? | paipeng | General Symbian C++ | 7 | 2007-03-28 20:58 |
| multitasking | siva12 | Carbide.c++ and CodeWarrior Tools | 2 | 2008-01-24 16:41 |
| Multiple asynchronous requests, active objects | fabiogr | General Symbian C++ | 2 | 2004-09-16 10:13 |
| What is the difference between an observer and an active object? | nokiandriver | General Symbian C++ | 2 | 2007-11-17 09:04 |
| How to stop a thread's active scheduler in another thread | cidhx | Symbian | 4 | 2004-01-10 03:16 |
