SQL Server 2008 R2

Asynchronous data synchronization lets a SQL Server Compact 3.5 application to continue processing other information while waiting for data synchronization to complete.

For example, suppose your application regularly performs data synchronization and sends lots of data over a slow connection. During the data synchronization, the application will not respond to user input. The user might think the application is not working correctly. To make the application more user-friendly during data synchronization, you might want to display a progress bar on the smart device that shows the status of synchronization. By using asynchronous data synchronization, you can initiate data synchronization and then trap synchronization events as they occur. Your application can then display these events to the user.

Using asynchronous data synchronization also lets you cancel a data synchronization that is already in progress.

If you are developing your SQL Server Compact 3.5 application by using the Microsoft .NET Compact Framework and one of the managed programming languages (Microsoft Visual Basic or Microsoft Visual C#), asynchronous calls are handled like other asynchronous calls in the Microsoft .NET Framework. If you are developing a native application by using Microsoft Visual C++ for Devices, you use the SyncStatusReport property of the ISSCEMerge interface.

Using the Managed API

There are three types in the System.Data.SqlServerCE namespace that are used in asynchronous data synchronization:

  • OnStartTableUpload

    This event occurs when each table in the subscription is uploaded to the Publisher.

  • OnStartTableDownload

    This event occurs when each table is returned from the Publisher.

  • OnSynchronization

    This event occurs when the reconciler is started, or when your code performs a synchronization check.

You also use the following three methods of the Replication object:

  • BeginSynchronize

    This method begins the synchronization process by using an IAsyncResultobject.

  • EndSynchronize

    You call EndSynchronize when the server returns an end synchronization message.

  • CancelSynchronize

    You can call the CancelSynchronize method to cancel the synchronization at any time.

The following steps show the process of performing asynchronous data synchronization with the managed API:

  1. Create a Replication object and set its properties.

  2. Create an IAsyncResult object and set it to the BeginSynchronize method of the Replication object.

  3. Pass the following properties to the BeginSynchronize method:

    • The callback method (AysncCallback event).

    • A method to handle the OnStartTableUpload event.

    • A method to handle the OnStartTableDownload event.

    • A method to handle the OnSynchronization event.

  4. Add code to the callback method to call the EndSynchronize method.

  5. Call the AsyncWaitHandle method of the IAsyncResult object.

  6. In the event handlers, you can add code to report the current status of the synchronization or to cancel the synchronization.

Status reports are sent during four events: at the start of each user table being processed for upload, at the start of each user table being processed for download, when the reconciler is started, and whenever the reconciler "percent complete" value is updated.

Using the Native API

As with synchronous data synchronization, you use the ISSCEMerge interface to perform asynchronous data synchronization. Unlike the process in managed code, asynchronous data synchronization is still a single-threaded process. Synchronization notifications are on the same thread as the synchronization; you use the SyncStatusReport property of the ISSCEMerge interface to retrieve synchronization status reports.