Finishes asynchronous execution of a SQL statement, returning the requested ULDataReader.
Public Function EndExecuteReader(
ByVal asyncResult As IAsyncResult
) As ULDataReader
public ULDataReader EndExecuteReader(IAsyncResult asyncResult)
asyncResult The System.IAsyncResult returned by the BeginExecuteReader call.
An ULDataReader object that can be used to retrieve the requested rows, which is the same behavior as the ExecuteReader method.
ArgumentException The asyncResult parameter is null (Nothing in Microsoft Visual Basic)
InvalidOperationException The EndExecuteReader method was called more than once for a single command execution, or the method was mismatched against its execution method.
You must call the EndExecuteReader method once for every call to the BeginExecuteReader method. The call must be after the BeginExecuteReader call returns. ADO.NET is not thread safe; it is your responsibility to ensure that the BeginExecuteReader method has returned. The System.IAsyncResult passed to the EndExecuteReader method must be the same as the one returned from the BeginExecuteReader call that is being completed. It is an error to call the EndExecuteReader method to end a BeginExecuteNonQuery call, and vice versa.
If an error occurs while executing the command, the exception is thrown when the EndExecuteReader method is called.
There are four ways to wait for execution to complete:
Call the EndExecuteReader method Calling the EndExecuteReader method blocks until the command completes. For example:
' Visual Basic Dim cmd As ULCommand = new ULCommand( _ "SELECT * FROM Departments", conn _ ) Dim res As IAsyncResult res = _ cmd.BeginExecuteReader() ' Perform other work ' This blocks until the command completes. Dim reader As ULDataReader = _ cmd.EndExecuteReader( res ) |
The following code is the C# language equivalent:
// C# ULCommand cmd = new ULCommand( "SELECT * FROM Departments", conn ); IAsyncResult res = cmd.BeginExecuteReader(); // Perform other work // This blocks until the command completes ULDataReader reader = cmd.EndExecuteReader( res ); |
Poll the IsCompleted property of the IAsyncResult You can poll the IsCompleted property of the IAsyncResult. For example:
' Visual Basic Dim cmd As ULCommand = new ULCommand( _ "SELECT * FROM Departments", conn _ ) Dim res As IAsyncResult res = _ cmd.BeginExecuteReader() While( !res.IsCompleted ) ' Perform other work End While ' This blocks 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 ) { // Perform other work. } // This blocks until the command completes. ULDataReader reader = cmd.EndExecuteReader( res ); |
Use the IAsyncResult.AsyncWaitHandle property to get a synchronization object You can use the IAsyncResult.AsyncWaitHandle property to get a synchronization object, and wait on that. For example:
' 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 does not block because the command is finished. Dim reader As ULDataReader = _ cmd.EndExecuteReader( res ) |
The following code is the C# language equivalent:
// C# ULCommand cmd = new ULCommand( "SELECT * FROM Departments", conn ); IAsyncResult res = cmd.BeginExecuteReader(); // Perform other work. WaitHandle wh = res.AsyncWaitHandle; wh.WaitOne(); // This does not block because the command is finished. ULDataReader reader = cmd.EndExecuteReader( res ); |
Specify a callback function when calling the BeginExecuteReader method You can specify a callback function when calling the BeginExecuteReader method. For example:
' 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 ' is called when the command completes. End Sub |
The following code is the C# language equivalent:
// 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 // is called when the command completes. } |
The callback function executes in a separate thread, so the usual caveats related to updating the user interface in a threaded program apply.
Discuss this page in DocCommentXchange.
|
Copyright © 2012, iAnywhere Solutions, Inc. - SQL Anywhere 12.0.1 |