Click here to view and discuss this page in DocCommentXchange. In the future, you will be sent there automatically.

SQL Anywhere 11.0.1 (中文) » UltraLite - .NET 编程 » UltraLite .NET 2.0 API 参考 » ULServerSyncListener 接口

 

ServerSyncInvoked 方法

在服务器启动的同步的 MobiLink 监听器调用应用程序执行同步时被调用。

语法
Visual Basic
Public Sub ServerSyncInvoked( _
   ByVal messageName As String _
)
C#
public void ServerSyncInvoked(
   string  messageName
);
参数
  • messageName   发送到应用程序的消息的名称。

注释

此方法由单独的线程调用。为避免多线程问题,它应向 UI 发布一个事件。如果您正在使用多线程,建议您使用一个单独的连接,并使用 lock 关键字来访问与应用程序的其余部分共享的所有对象。

示例

以下代码段介绍如何接收服务器同步请求并在 UI 线程中执行同步。

' Visual Basic
Imports iAnywhere.Data.UltraLite
   
Public Class MainWindow
  Inherits System.Windows.Forms.Form
  Implements ULServerSyncListener
  Private conn As ULConnection

  Public Sub New(ByVal args() As String)

    MyBase.New()

    'This call is required by the Windows Form Designer.
    InitializeComponent()

    'Add any initialization after the InitializeComponent() call
    ULConnection.DatabaseManager.SetServerSyncListener( _
        "myCompany.mymsg", "myCompany.myapp", Me _
      )
    'Create Connection
    ...
  End Sub

  Protected Overrides Sub OnClosing( _
        ByVal e As System.ComponentModel.CancelEventArgs _
      )
    ULConnection.DatabaseManager.SetServerSyncListener( _
        Nothing, Nothing, Nothing _
      )
    MyBase.OnClosing(e)
  End Sub

  Public Sub ServerSyncInvoked(ByVal messageName As String) _
      Implements ULServerSyncListener.ServerSyncInvoked
    Me.Invoke(New EventHandler(AddressOf Me.ServerSyncAction))
  End Sub

  Public Sub ServerSyncAction( _
        ByVal sender As Object, ByVal e As EventArgs _
      )
    ' Do Server sync
    conn.Synchronize()
  End Sub
End Class
// C#
using iAnywhere.Data.UltraLite;
public class Form1 : System.Windows.Forms.Form, ULServerSyncListener
{
  private System.Windows.Forms.MainMenu mainMenu1;
  private ULConnection conn;

  public Form1()
  {
    //
    // Required for Windows Form Designer support
    //
    InitializeComponent();

    //
    // TODO: Add any constructor code after
    // InitializeComponent call
    //
    ULConnection.DatabaseManager.SetServerSyncListener(
        "myCompnay.mymsg", "myCompany.myapp", this
      );
    // Create connection
    ...
  }

  protected override void Dispose( bool disposing )
  {
    base.Dispose( disposing );
  }

  protected override void OnClosing(
      System.ComponentModel.CancelEventArgs e
    )
  {
    ULConnection.DatabaseManager.SetServerSyncListener(
        null, null, null
      );
    base.OnClosing(e);
  }

  public void ServerSyncInvoked( string messageName )
  {
    this.Invoke( new EventHandler( ServerSyncHandler ) );
  }

  internal void ServerSyncHandler(object sender, EventArgs e)
  {
    conn.Synchronize();
  }
}
另请参见