In Symbian, ao plays an important role in completing multiple tasks and many asynchronous operations. Theoretically, it can completely replace multithreading in general systems, but Symbian retains multithreading for us. In fact, in some cases, we still need multithreading. If some complex recursive processes cannot be split into AO, we can only use multiple threads.
When multithreading is used, we should pay attention to the handling of abnormal exit. For example, if the user presses the exit key while the background task is being executed, we should handle the problem properly at this time. Generally, we need to call cancel () in actual coding, we generally process multithreading in an AO, because the multi-thread notification mechanism in Symbian is completed by using the active object. We call rthread :: logon (istatus) is used to set istatus to krequestpending, so that the completion request is used to notify the thread of completion. If the thread is accidentally interrupted, we can call cancel () for processing, we should always interrupt and close the thread in the overload of the docancel () function, such:
RThread thread ;thread.Open(ThreadID) ;thread.Kill(KErrCancel) ;thread.Close() ;
In this way, we will not encounter errors when exiting. We should also note that if the thread has been closed, we will call the thread again. close () will report the Kern-exec 3 error, which indicates that a blank handler is mistakenly operated, which must be avoided.
Let's take a look at ao. AO can complete many complex tasks, such as using a package thread, using its own runl () to respond to the Operations completed by the thread, or directly acting as a signal lamp. Regardless of the operation, we need to note that when using user: requestcomplete (), we must set istatus to krequestpending. This is very important, as well as requestcomplete () or the execution fails.
In addition, after setting a request, call the setactive () function to notify scheduler of a waiting request. after calling the setactive () function, we must complete the request, otherwise this request will be an stray request event that will cause an error, for example, the next call to setactive () will produce the E32User-CBase 42 error, we use isactive () the judgment is also very important to avoid errors in this regard :)
In short, ao should be cautious and bold in its use. Do not forget runl (), docancel (), and the setting of cactive priority in the constructor :)
The last thing to note is how to notify the AO request of another thread in the thread to complete. If the request is to notify the main thread, we need to get the ID of the main thread, this can be obtained by calling rthread: ID (), for example:
RThread trd;iThreadParam.iThreadId = trd.Id();trd.Close();
In this way, we can use rthread: requestcomplete () in another thread to notify AO of the main thread. Haha, if it is another thread, we can use the thread name :)
In general, the use of threads and AO should be careful :)