Article ID: 831883 - Last Review: November 29, 2007 - Revision: 1.5 HOWTO: Initialize RAPI Asynchronously with CeRapiInitEx()On This PageSUMMARYDesktop applications call the CeRapiInitEx function to initialize Remote API (RAPI) before the applications
make any RAPI calls to communicate with Pocket PC or Microsoft Smartphone
devices. CeRapiInitEx can be called asynchronously by passing in a valid pointer to the
RAPIINIT structure, and the caller can determine how long it wants to wait
before RAPI is initialized by using the WaitForSingleObject function. In Microsoft ActiveSync 3.7 and earlier, RAPI was designed to be initialized on threads with message pumps. RAPI was not designed to be initialized asychronously on worker threads in client applications. Microsoft does not recommend that you use unofficial workarounds to make RAPI initialize asychronously on worker threads in client applications. The RAPI thread model was changed in ActiveSync 3.7.1 so that it can be freely initialized on either message pump threads (UI threads) or on simple worker threads. Therefore, if you use CeRapiInitEx correctly, no workaround is necessary and you will be able to initialize RAPI in any context. As a side effect, unofficial workarounds will not work. This article includes samples for how to correctly use CeRapiInitEx to initialize RAPI asynchronously. Note To initialize RAPI synchronously, make a blocking call by using the CeRapiInit function. The behavior change in ActiveSync 3.7.1 does not affect applications that call CeRapiInit to initialize RAPI synchronously. MORE INFORMATIONExample of Incorrect Workaround CodeThis sample code demonstrates a typical unofficial workaround that worked in ActiveSync 3.7 or earlier. It is an inefficient solution because it calls the CeRapiInitEx and CeRapiUninit functions many times. This code does not work in ActiveSync 3.7.1 or later because the thread model inside RAPI was changed.ExamplesThe following code examples demonstrate how to initialize RAPI asynchronously. These examples are not the only method of initializing RAPI asynchronously, but they demonstrate the general principle: Call CeRapiInitEx one time, then wait for the RAPI event to initialize based on your requirements. You may use a timeout value, a timer, or other event mechanisms to wait for initialization to finish.Note The samples are based on ActiveSync 3.7.1. If you want to make sure your application works fine with both ActiveSync 3.7.1 and earlier version of ActiveSync, you must initialize RAPI on a thread with a message pump. For more information, see Example 2. Example 1 To initialize RAPI asynchronously in a simple worker thread, use the following code. In the code, the CeRapiUninit function is called to uninitialize RAPI if the WaitForSingleObject function returns a value that is other than WAIT_OBJECT_0. This behavior is most likely caused by a timeout event (in this example, the timeout is 30 seconds). To initialize RAPI asynchronously in a UI thread (a thread with a message pump), abort when timeout To initialize RAPI asynchronously in a UI thread, use the following code. In the code, the CeRapiUninit function is called to uninitialize RAPI if the MsgWaitForMultipleObjects function returns a value that is other than WAIT_OBJECT_0. This behavior is most likely caused by a timeout (in this example, the timeout is 30 seconds) or if any message is in the queue. REFERENCES For more information about ActiveSync and RAPI, visit the
following Microsoft Developer Network (MSDN) Web sites: http://msdn2.microsoft.com/en-us/library/ms879784.aspx
(http://msdn2.microsoft.com/en-us/library/ms879784.aspx)
http://msdn2.microsoft.com/en-us/library/ms860198.aspx (http://msdn2.microsoft.com/en-us/library/ms860198.aspx) | Article Translations
|
Back to the top
