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
'Usage
Dim instance As SqlCeReplication
Dim onSyncCompletion As AsyncCallback
Dim state As Object
Dim returnValue As IAsyncResult

returnValue = instance.BeginSynchronize(onSyncCompletion, _
    state)
public IAsyncResult BeginSynchronize(
    AsyncCallback onSyncCompletion,
    Object state
)
public:
IAsyncResult^ BeginSynchronize(
    AsyncCallback^ onSyncCompletion, 
    Object^ state
)
member BeginSynchronize : 
        onSyncCompletion:AsyncCallback * 
        state:Object -> IAsyncResult 
public function BeginSynchronize(
    onSyncCompletion : AsyncCallback, 
    state : Object
) : IAsyncResult

Parameters

Return Value

Type: System. . :: . .IAsyncResult
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 , see "Asynchronous Data Synchronization" in the Books Online.

Examples

The following example shows how to set up 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();
    }
}

See Also

Reference

SqlCeReplication Class

SqlCeReplication Members

BeginSynchronize Overload

System.Data.SqlServerCe Namespace