BeginSynchronize 方法 (AsyncCallback, Object)
啟動非同步資料的同步處理作業。當同步處理結束時,將呼叫 AsyncCallback 委派。在同步處理期間,不會執行狀態報告。
命名空間: System.Data.SqlServerCe
組件: System.Data.SqlServerCe (在 System.Data.SqlServerCe.dll 中)
語法
'宣告
Public Function BeginSynchronize ( _
onSyncCompletion As AsyncCallback, _
state As Object _
) As IAsyncResult
'用途
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
參數
- onSyncCompletion
型別:System. . :: . .AsyncCallback
由呼叫端實作並於同步處理結束時呼叫的 IAsyncResult 委派。
- state
型別:System. . :: . .Object
AsyncState 屬性傳回之使用者定義的物件。
傳回值
型別:System. . :: . .IAsyncResult
非同步作業的 IAsyncResult 介面,已透過呼叫這個函數來啟動。您可以使用這個介面,測試同步處理是否完成,或是等候同步處理結束。
備註
如需有關 SQL Server Compact 3.5 中非同步資料同步處理的詳細資訊,請參閱《SQL Server Compact 3.5 線上叢書》內的<非同步資料同步處理>。
範例
下列範例會示範如何設定 SQL Server Compact 3.5 複寫,以執行非同步的資料同步處理。
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();
}
}