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 参考 » ULCommand 类

 

EndExecuteReader 方法

完成 SQL 语句的异步执行,并返回所请求的 ULDataReader。

语法
Visual Basic
Public Function EndExecuteReader( _
   ByVal asyncResult As IAsyncResult _
) As ULDataReader
C#
public ULDataReader EndExecuteReader(
   IAsyncResult asyncResult
);
参数
  • asyncResult   调用 BeginExecuteReader 而返回的 System.IAsyncResult。

返回值

可用于检索所请求行的 ULDataReader 对象(与 ExecuteReader 的行为相同)。

注释

每次调用 BeginExecuteReader 时都必须调用一次 EndExecuteReader。必须在 BeginExecuteReader 返回之后执行该调用。ADO.NET 不是线程安全的;由您负责来确保 BeginExecuteReader 已返回。传递给 EndExecuteReader 的 System.IAsyncResult 必须与从即将完成的 BeginExecuteReader 调用返回的 System.IAsyncResult 相同。通过调用 EndExecuteReader 来结束对 BeginExecuteNonQuery 的调用是错误的,反之亦然。

如果在执行命令时出现错误,则调用 EndExecuteReader 时会抛出异常。

等待执行完成有四种方法:

Call EndExecuteReader 在命令完成后再调用 EndExecuteReader 块。例如:

' Visual Basic
Dim cmd As ULCommand = new ULCommand( _
    "SELECT * FROM Departments", conn _
  )
Dim res As IAsyncResult res = _
  cmd.BeginExecuteReader()
' perform other work
' this will block until the command completes
Dim reader As ULDataReader = _
  cmd.EndExecuteReader( res )

// C#
ULCommand cmd = new ULCommand(
    "SELECT * FROM Departments", conn
  );
IAsyncResult res = cmd.BeginExecuteReader();
// perform other work
// this will block until the command completes
ULDataReader reader = cmd.EndExecuteReader( res );

Poll the IsCompleted property of the IAsyncResult 可以轮询 IAsyncResult 的 IsCompleted 属性。例如:

' Visual Basic
Dim cmd As ULCommand = new ULCommand( _
    "SELECT * FROM Departments", conn _
  )
Dim res As IAsyncResult res = _
  cmd.BeginExecuteReader()
While( !res.IsCompleted )
  ' do other work
End While
' this will block until the command completes
Dim reader As ULDataReader = _
  cmd.EndExecuteReader( res )

// C#
ULCommand cmd = new ULCommand(
    "SELECT * FROM Departments", conn
  );
IAsyncResult res = cmd.BeginExecuteReader();
while( !res.IsCompleted ) {
  // do other work
}
// this will block until the command completes
ULDataReader reader = cmd.EndExecuteReader( res );

Use the IAsyncResult.AsyncWaitHandle property to get a synchronization object 可以使用 IAsyncResult.AsyncWaitHandle 属性获取同步对象,并进行等待。例如:

' Visual Basic
Dim cmd As ULCommand = new ULCommand( _
    "SELECT * FROM Departments", conn _
  )
Dim res As IAsyncResult res = _
  cmd.BeginExecuteReader()
' perform other work
Dim wh As WaitHandle = res.AsyncWaitHandle
wh.WaitOne()
' this will not block because the command is finished
Dim reader As ULDataReader = _
  cmd.EndExecuteReader( res )

// C#
ULCommand cmd = new ULCommand(
    "SELECT * FROM Departments", conn
  );
IAsyncResult res = cmd.BeginExecuteReader();
// perform other work
WaitHandle wh = res.AsyncWaitHandle;
wh.WaitOne();
// this will not block because the command is finished
ULDataReader reader = cmd.EndExecuteReader( res );

Specify a callback function when calling BeginExecuteReader 可在调用 BeginExecuteReader 时指定回调函数。例如:

' Visual Basic
Private Sub callbackFunction(ByVal ar As IAsyncResult)
  Dim cmd As ULCommand = _
    CType(ar.AsyncState, ULCommand)
  ' this won't block since the command has completed
  Dim reader As ULDataReader = cmd.EndExecuteReader()
End Sub
' elsewhere in the code
Private Sub DoStuff() 
  Dim cmd As ULCommand = new ULCommand( _
      "SELECT * FROM Departments", conn _
    )
  Dim res As IAsyncResult = _
    cmd.BeginExecuteReader( _
        callbackFunction, cmd _
      )
  ' perform other work.  The callback function 
  ' will be called when the command completes
End Sub
// C#
private void callbackFunction( IAsyncResult ar )
{
  ULCommand cmd = (ULCommand) ar.AsyncState;
  // this won't block since the command has completed
  ULDataReader reader = cmd.EndExecuteReader();
}
// elsewhere in the code
private void DoStuff() 
{
  ULCommand cmd = new ULCommand(
      "SELECT * FROM Departments", conn
    );
  IAsyncResult res = cmd.BeginExecuteReader(
      callbackFunction, cmd
    );
  // perform other work.  The callback function 
  // will be called when the command completes
}

回调函数在单独的线程中执行,因此与在线程化程序中更新用户界面有关的常见告诫也适用。

另请参见