Share via


SqlCeReplication.BeginSynchronize Method (AsyncCallback, Object)

Starts an asynchronous data synchronization operation. When synchronization ends, the AsyncCallback delegates are called. During synchronization, no status reporting is performed.

Namespace: System.Data.SqlServerCe
Assembly: System.Data.SqlServerCe (in system.data.sqlserverce.dll)

Syntax

'Declaration
Public Function BeginSynchronize ( _
    onSyncCompletion As AsyncCallback, _
    state As Object _
) As IAsyncResult
public IAsyncResult BeginSynchronize (
    AsyncCallback onSyncCompletion,
    Object state
)
public:
IAsyncResult^ BeginSynchronize (
    AsyncCallback^ onSyncCompletion, 
    Object^ state
)
public IAsyncResult BeginSynchronize (
    AsyncCallback onSyncCompletion, 
    Object state
)
public function BeginSynchronize (
    onSyncCompletion : AsyncCallback, 
    state : Object
) : IAsyncResult

Parameters

  • onSyncCompletion
    The IAsyncResult delegates that are implemented by the caller and called at the end of synchronization.
  • state
    A user-defined object that is returned by the AsyncState property.

Return Value

The IAsyncResult interface for the asynchronous operation that has been started by calling this function. You can use this interface for testing for completion, or waiting until synchronization ends.

Remarks

For information about asynchronous data synchronization in SQL Server Compact 3.5 SP1, see "Asynchronous Data Synchronization" in the SQL Server Compact 3.5 SP1 Books Online.

Example

The following example shows how to set up SQL Server Compact 3.5 SP1 replication to perform asynchronous data synchronization.

Public Sub SyncCompletedCallback(ByVal ar As IAsyncResult)
    Try
        Dim repl As SqlCeReplication = CType(ar.AsyncState, SqlCeReplication)

        ' Complete the asynchronous sync and test for errors
        '
        repl.EndSynchronize(ar)
    Catch
        ' Handle errors here
        '
    End Try

End Sub 'SyncCompletedCallback

Public Sub Test()
    Dim repl As SqlCeReplication = Nothing

    Try
        ' Set the Replication object
        '
        'NOTE: when possible, prompt users to enter security 
        'credentials at runtime. If you store credentials in a file, 
        'you must secure the file to prevent unauthorized access.
        '
        repl = New SqlCeReplication( _
            "https://www.adventure-works.com/sqlmobile/sqlcesa35.dll", _
            "MyInternetLogin", _
            "<enterStrongPassword>", _
            "MyPublisher", _
            "MyPublisherDatabase", _
            "MyPublisherLogin", _
            "<enterStrongPassword>", _
            "MyPublication", _
            "MySubscriber", _
            "Data Source=MyDatabase.sdf")

        ' Begin asynchronous sync; This call returns immediately 
        '
        Dim ar As IAsyncResult = repl.BeginSynchronize( _
            New AsyncCallback(AddressOf SyncCompletedCallback), _
            repl)

        Thread.Sleep(3000)

        ' Cancel the sync if it didn't complete in 3 seconds; normally,
        ' this is hooked up to a button's Click event
        '
        repl.CancelSynchronize()
    Catch
        ' Handle errors here
    Finally
        ' Dispose the repl object
        '
        repl.Dispose()
    End Try

End Sub 'Test
public void SyncCompletedCallback(IAsyncResult ar)
{
    try
    {
        SqlCeReplication repl = (SqlCeReplication)ar.AsyncState;

        // Complete the asynchronous sync and test for errors
        //
        repl.EndSynchronize(ar);
    }
    catch (SqlCeException)
    {
        // Handle errors here
        //
    }
}

public void Test()
{
    SqlCeReplication repl = null;

    try
    {
        // Set the Replication object
        //
        //NOTE: when possible, prompt users to enter security 
        //credentials at runtime. If you store credentials in a file, 
        //you must secure the file to prevent unauthorized access.
        //
        repl = new SqlCeReplication(
            "https://www.adventure-works.com/sqlmobile/sqlcesa35.dll",
            "MyInternetLogin",
            "<enterStrongPassword>",
            "MyPublisher",
            "MyPublisherDatabase",
            "MyPublisherLogin",
            "<enterStrongPassword>",
            "MyPublication",
            "MySubscriber",
            "Data Source=MyDatabase.sdf");

        // Begin asynchronous sync; This call returns immediately 
        //
        IAsyncResult ar = repl.BeginSynchronize(
            new AsyncCallback(SyncCompletedCallback),
            repl);

        Thread.Sleep(3000);

        // Cancel the sync if it didn't complete in 3 seconds; normally,
        // this is hooked up to a button's Click event
        //
        repl.CancelSynchronize();
    }
    catch (SqlCeException)
    {
        // Handle errors here
    }
    finally
    {
        // Dispose the repl object
        //
        repl.Dispose();
    }
}

Thread Safety

Any public static (Shared in Microsoft Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Platforms

Development Platforms

Windows Vista, Windows Mobile 5.0, Windows XP Professional with Service Pack 2 (SP2), Windows Server 2003, Windows Mobile 2003 for Pocket PC, Windows CE 5.0
Version Information
.NET Framework and NET Compact Framework
Supported in 3.5
.NET Framework
Supported in 3.0
.NET Compact Framework and .Net Framework
Supported in 2.0

See Also

Reference

SqlCeReplication Class
SqlCeReplication Members
System.Data.SqlServerCe Namespace