Главная » Просмотр файлов » Wiley.Developing.Software.for.Symbian.OS.2nd.Edition.Dec.2007

Wiley.Developing.Software.for.Symbian.OS.2nd.Edition.Dec.2007 (779887), страница 53

Файл №779887 Wiley.Developing.Software.for.Symbian.OS.2nd.Edition.Dec.2007 (Symbian Books) 53 страницаWiley.Developing.Software.for.Symbian.OS.2nd.Edition.Dec.2007 (779887) страница 532018-01-10СтудИзба
Просмтор этого файла доступен только зарегистрированным пользователям. Но у нас супер быстрая регистрация: достаточно только электронной почты!

Текст из файла (страница 53)

In the header file, I also defined KSimpleExText as:_LIT(KSimpleExText,"Simple Example AO");ConstructL() sets this text to the screen (and the active object restoresit when the countdown is terminated).ACTIVE OBJECT ISSUES2698.9 Active Object IssuesUsing active objects is the best way to handle events from multipleasynchronous functions and to implement multiple (though non-preemptive) threads in Symbian OS. However, there are issues that canoccur, and many programmers struggle with active objects at first.

Havinga good understanding of how they work helps avoid problems.8.9.1 Do Not Block in an Active ObjectOnce the active scheduler is started, the only safe blocking that can occurin the thread is when the active scheduler event loop waits at the thread’srequest semaphore for an event. However, if you call an operation thatblocks inside an event handler (i.e., in your RunL() function), such aswaiting for a semaphore, or calling a lengthy blocking function (such asUser::After()), then not only do you block your RunL(), but youalso block the entire thread, including the active scheduler and all therest of the thread’s active objects.

That’s not to say that you cannot blockat all – sometimes a very short thread-blocking operation is needed – butyou have to keep in mind that, during that block, no other asynchronousfunction events are being handled.This is why you should not block in GUI programs (or if you must block,keep it to 10 ms or less).

The GUI event handler, HandleCommandL(),is actually invoked from a RunL() of an active object that the applicationUI class inherits from. So if you delay for some reason when handling aGUI event, your GUI will become non-responsive during this delay sincethe GUI program’s active scheduler cannot process further events.It is possible to block in an active object, yet still have events processedusing nested CActiveScheduler::Start() and Stop() calls. Nesting active scheduler calls can be very complex to implement correctlythough, and their use is generally discouraged, and beyond the scope ofthis book.8.9.2 Avoid Stray-Signal PanicsAny Symbian programmer who has developed active objects will probably have encountered a E32USER-CBase 46 panic. This is the straysignal panic and it is invoked by the active scheduler’s event loopwhen it receives a signal at the thread’s request semaphore, but cannot find an active object that it belongs to (i.e., no active objects withiActive==ETrue AND iStatus!=KRequestPending).

There aremany situations that can cause this. Here are just a few:•Not adding your active object to the scheduler (via CActiveScheduler::Add())270ASYNCHRONOUS FUNCTIONS AND ACTIVE OBJECTSIf you invoke the asynchronous function in your active object, yet it isnot added to the scheduler, then a stray-signal panic will occur whenthe function completes, since the scheduler does not know about youractive object.•Not calling SetActive()If you do not call SetActive() in your active object when invokingthe asynchronous function, then, when the signal comes in, thescheduler does not see your active object’s iActive flag set, andtherefore does not consider it a target for the event.

It finds that theevent belongs to no active object and generates the stray-signal panic.•Not calling User::WaitForRequest() on an asynchronous function not associated with an active objectIn some cases, you may want to use User::WaitForRequest() towait for an asynchronous function that is not associated with an activeobject to complete (although remember that this also blocks youractive object’s event loop).

If you call such an asynchronous function,but forget to consume its event via User::WaitForRequest(),then the active scheduler will eventually get the event and, since itwill not find the active object the event belongs to, the scheduler willgenerate the stray signal.A common cause of a stray-signal panic is when you cancel anasynchronous function that is not associated with an active object.Remember that a cancel also generates an event (it’s a common error thatthis is not considered), and a stray signal results.8.9.3 Have One Outstanding Event at a TimeOn the surface it may seem that you can call as many asynchronousfunctions as you like in an active object (calling SetActive() aftereach) and assume that your RunL() is invoked as each asynchronousfunction completes. However, this is not true.

An active object can onlyhave one outstanding request at a time, and if you try to have more, astray signal will result.When you think about how the scheduler works, you will see whythis is so. The only thing that SetActive() does is to set your activeobject’s iActive flag to ETrue. So when you invoke the first asynchronous function and call SetActive(), you have iActive==ETrueand iStatus==KRequestPending.

If you then invoke a second asynchronous function from your active object, and call SetActive(),iActive is already set from the previous request (it just sets it again).The first asynchronous function that completes generates a signal at therequest semaphore and the active scheduler calls the active object’sRunL() function. However, after that RunL() completes, the iActiveUSING ACTIVE OBJECTS FOR BACKGROUND TASKS271flag is cleared by CActive – but there is still an outstanding functioncall since you invoked multiple asynchronous function calls.

So whenthe second asynchronous function completes, the active scheduler seesyour iActive flag set to zero, and, finding no other active object for itto belong to, generates a stray-signal panic.8.10 Using Active Objects for Background TasksThus far we have discussed active objects as being simply a way to startasynchronous functions and handle their completion, but you can alsouse them to perform background processing as you can with threads.This example illustrates that concept. It also illustrates other conceptsmentioned earlier, such as creating, starting, and stopping the activescheduler.The example creates two active objects whose RunL() functions areinvoked at specified intervals. This example uses the console and is builtas an EXE executable that can be run on the emulator (see section 6.1 forhow to create a console program).8.10.1 CTimerInstead of deriving our active objects straight from CActive, this examplederives them from an API class called CTimer, which is itself derived fromCActive.

CTimer uses RTimer to generate events after a user-definedperiod of time. Using CTimer is more convenient than implementingRTimer within our own active object.CTimer implements the following method, which I will use in theexample:void After(TTineIntervalMicroSeconds32, aInterval)CTimer::After() simply invokes its associated RTimer’s After()method and calls CActive::SetActive(). RunL() (implemented inCTimer’s derived class) is called when the timer expires.Following is the header file.#ifndef _ACTIVEH#define _ACTIVEH#include <e32base.h>#include <e32cons.h>CConsoleBase* Console;class CPrimaryTask : public CTimer{public:static CPrimaryTask* NewLC();272ASYNCHRONOUS FUNCTIONS AND ACTIVE OBJECTSvoid StartRunning(TInt aRepeat, TInt aInterval);protected:void RunL();CPrimaryTask() : CTimer(EPriorityStandard){ }private:TInt iInterval;TInt iRepeatCnt;};class CBackground : public CTimer{public:static CBackground* NewLC();void StartBackground(TInt aInterval);protected:void RunL();CBackground() : CTimer(EPriorityLow){ }private:TInt iInterval;};_LIT(KActiveExName,"Active Object Example");void ActiveExampleL();#endifThere are two active objects in this example – a primary task anda background task.

The following shows their implementation, and themain program.#include <e32base.h>#include <e32cons.h>#include <e32debug.h>#include "active.h"/*----------------------------------------------------Active Object: CPrimaryTask------------------------------------------------------*/CPrimaryTask* CPrimaryTask::NewLC(){CPrimaryTask* self=new (ELeave) CPrimaryTask;CleanupStack::PushL(self);self->ConstructL(); // Need to call CTimer::ConstructL()CActiveScheduler::Add(self);return self;}void CPrimaryTask::StartRunning(TInt aRepeat, TInt aInterval){iRepeatCnt=aRepeat;iInterval=aInterval;CTimer::After(iInterval);}void CPrimaryTask::RunL(){if (iRepeatCnt-- > 0){_LIT(KFirstActiveWaiting,"CPrimaryTask: RunL");_LIT(KFormatWithNewLine, "%S\n");gConsole->Printf(KFormatWithNewLine,&KFirstActiveWaiting);USING ACTIVE OBJECTS FOR BACKGROUND TASKS273RDebug::Print(KFirstActiveWaiting);...// do task processing hereAfter(iInterval); // reissue request}else{CActiveScheduler::Stop();// repeat count expired// end event loop to exit program}}/*----------------------------------------------------Active Object: CBackground------------------------------------------------------*/void CBackground::StartBackground(TInt aInterval){iInterval=aInterval;CTimer::After(iInterval);}CBackground* CBackground::NewLC(){CBackground* self=new (ELeave) CBackground;CleanupStack::PushL(self);self->ConstructL();// Need to call CTimer::ConstructL()CActiveScheduler::Add(self);return self;}void CBackground::RunL(){_LIT(KBackgroundTaskMsg,"CBackground RunL");_LIT(KFormatWithNewLine, "%S\n");gConsole->Printf(KFormatWithNewLine,&KBackgroundTaskMsg);RDebug::Print(KBackgroundTaskMsg);...// do task processing hereAfter(iInterval);}/*----------------------------------------------------E32Main() Entry point------------------------------------------------------*/TInt E32Main(){__UHEAP_MARK;CTrapCleanup* cleanStack=CTrapCleanup::New(); // create a clean-up stackTRAPD(leaveCode,ActiveExampleL()); // more initialization,// then do exampleif ( leaveCode != KErrNone)User::Panic(KActiveExName,leaveCode);delete cleanStack; // cleanup cleanup stack__UHEAP_MARKEND;return 0; // and return}/*---------------------------------------------Active Object Example main function----------------------------------------------*/void ActiveExampleL()274ASYNCHRONOUS FUNCTIONS AND ACTIVE OBJECTS{/*---------------------------------------------------------Create a full screen console----------------------------------------------------------*/_LIT(KConsoleTitle,"Console");gConsole=Console::NewL(KConsoleTitle,TSize(KConsFullScreen,KConsFullScreen));CleanupStack::PushL(Console);gConsole->Printf(_L("Active Object Example"));gConsole->Printf(_L("Press Key to Begin"));gConsole->Getch();/*-----------------------------------------------Create and install Active Scheduler------------------------------------------------*/CActiveScheduler* mySched = new (ELeave) CActiveScheduler;CleanupStack::PushL(mySched);CActiveScheduler::Install(mySched);/*---------------------------------------------Start active objects-----------------------------------------------*/CPrimaryTask *firstAct = CPrimaryTask::NewLC();CBackground *backAct = CBackground::NewLC();backAct->StartBackground(4000000);// Run every 4 secondsfirstAct->StartRunning(20,1000000); // Run for 20 times, 1 sec interval/*---------------------------------------------Start the active scheduler event loop.----------------------------------------------*/CActiveScheduler::Start();/*-------------------------------------------// Event loop exited, cleanup--------------------------------------------*/CleanupStack::PopAndDestroy(3); // Clean up backAct, //firstAct andmySched_LIT(KActiveComplete,"Scheduler exited, cleanup");_LIT(KFormatWithNewLine, "%S\n");gConsole->Printf(KFormatWithNewLine,&KActiveComplete);RDebug::Print(KActiveComplete);_LIT(KPressKeyToExit,"[Press any key] to exit");gConsole->Printf(KPressKeyToExit);gConsole->Getch();CleanupStack::PopAndDestroy(gConsole);}This example starts two repeating tasks, implemented via active objectclasses CPrimaryTask and CBackground.

Характеристики

Тип файла
PDF-файл
Размер
3,36 Mb
Материал
Тип материала
Высшее учебное заведение

Список файлов книги

Свежие статьи
Популярно сейчас
Зачем заказывать выполнение своего задания, если оно уже было выполнено много много раз? Его можно просто купить или даже скачать бесплатно на СтудИзбе. Найдите нужный учебный материал у нас!
Ответы на популярные вопросы
Да! Наши авторы собирают и выкладывают те работы, которые сдаются в Вашем учебном заведении ежегодно и уже проверены преподавателями.
Да! У нас любой человек может выложить любую учебную работу и зарабатывать на её продажах! Но каждый учебный материал публикуется только после тщательной проверки администрацией.
Вернём деньги! А если быть более точными, то автору даётся немного времени на исправление, а если не исправит или выйдет время, то вернём деньги в полном объёме!
Да! На равне с готовыми студенческими работами у нас продаются услуги. Цены на услуги видны сразу, то есть Вам нужно только указать параметры и сразу можно оплачивать.
Отзывы студентов
Ставлю 10/10
Все нравится, очень удобный сайт, помогает в учебе. Кроме этого, можно заработать самому, выставляя готовые учебные материалы на продажу здесь. Рейтинги и отзывы на преподавателей очень помогают сориентироваться в начале нового семестра. Спасибо за такую функцию. Ставлю максимальную оценку.
Лучшая платформа для успешной сдачи сессии
Познакомился со СтудИзбой благодаря своему другу, очень нравится интерфейс, количество доступных файлов, цена, в общем, все прекрасно. Даже сам продаю какие-то свои работы.
Студизба ван лав ❤
Очень офигенный сайт для студентов. Много полезных учебных материалов. Пользуюсь студизбой с октября 2021 года. Серьёзных нареканий нет. Хотелось бы, что бы ввели подписочную модель и сделали материалы дешевле 300 рублей в рамках подписки бесплатными.
Отличный сайт
Лично меня всё устраивает - и покупка, и продажа; и цены, и возможность предпросмотра куска файла, и обилие бесплатных файлов (в подборках по авторам, читай, ВУЗам и факультетам). Есть определённые баги, но всё решаемо, да и администраторы реагируют в течение суток.
Маленький отзыв о большом помощнике!
Студизба спасает в те моменты, когда сроки горят, а работ накопилось достаточно. Довольно удобный сайт с простой навигацией и огромным количеством материалов.
Студ. Изба как крупнейший сборник работ для студентов
Тут дофига бывает всего полезного. Печально, что бывают предметы по которым даже одного бесплатного решения нет, но это скорее вопрос к студентам. В остальном всё здорово.
Спасательный островок
Если уже не успеваешь разобраться или застрял на каком-то задание поможет тебе быстро и недорого решить твою проблему.
Всё и так отлично
Всё очень удобно. Особенно круто, что есть система бонусов и можно выводить остатки денег. Очень много качественных бесплатных файлов.
Отзыв о системе "Студизба"
Отличная платформа для распространения работ, востребованных студентами. Хорошо налаженная и качественная работа сайта, огромная база заданий и аудитория.
Отличный помощник
Отличный сайт с кучей полезных файлов, позволяющий найти много методичек / учебников / отзывов о вузах и преподователях.
Отлично помогает студентам в любой момент для решения трудных и незамедлительных задач
Хотелось бы больше конкретной информации о преподавателях. А так в принципе хороший сайт, всегда им пользуюсь и ни разу не было желания прекратить. Хороший сайт для помощи студентам, удобный и приятный интерфейс. Из недостатков можно выделить только отсутствия небольшого количества файлов.
Спасибо за шикарный сайт
Великолепный сайт на котором студент за не большие деньги может найти помощь с дз, проектами курсовыми, лабораторными, а также узнать отзывы на преподавателей и бесплатно скачать пособия.
Популярные преподаватели
Добавляйте материалы
и зарабатывайте!
Продажи идут автоматически
6455
Авторов
на СтудИзбе
305
Средний доход
с одного платного файла
Обучение Подробнее