Beendet die asynchrone Ausführung einer SQL-Anweisung und gibt das angeforderte ULDataReader-Objekt zurück
Visual Basic Public Function EndExecuteReader( _ ByVal asyncResult As IAsyncResult _ ) As ULDataReader
C# public ULDataReader EndExecuteReader( IAsyncResult asyncResult );
asyncResult Das System.IAsyncResult-Objekt, das vom Aufruf an BeginExecuteReader zurückgegeben wird
Ein ULDataReader-Objekt, das für die Abfrage der angeforderten Zeilen verwendet werden kann (dasselbe Verhalten wie ExecuteReader)
EndExecuteReader muss für jeden Aufruf von BeginExecuteReader ein Mal aufgerufen werden. Der Aufruf muss erfolgen, nachdem BeginExecuteReader ausgeführt wurde. ADO.NET ist nicht threadsicher. Sie müssen dafür Sorge tragen, dass BeginExecuteReader abgeschlossen wurde. Das System.IAsyncResult-Objekt, das an EndExecuteReader übergeben wurde, muss mit dem des ausgeführten BeginExecuteReader-Aufrufs übereinstimmen. Es wird ein Fehler erzeugt, wenn EndExecuteReader aufgerufen wird, um einen Aufruf von BeginExecuteNonQuery zu beenden, und umgekehrt.
Wenn während der Ausführung des Befehls ein Fehler auftritt, wird eine Ausnahmebedingung generiert, wenn EndExecuteReader aufgerufen wird.
Es gibt vier Möglichkeiten, auf den Abschluss der Ausführung zu warten:
EndExecuteReader aufrufen Der Aufruf von EndExecuteReader wird blockiert, bis der Befehl ausgeführt wurde. Zum Beispiel:
' 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 ); |
Eigenschaft IsCompleted von IAsyncResult abrufen Sie können die Eigenschaft IsCompleted von IAsyncResult abrufen. Zum Beispiel:
' 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 ); |
Eigenschaft IAsyncResult.AsyncWaitHandle verwenden, um ein Synchronisationsobjekt abzurufen Sie können die Eigenschaft IAsyncResult.AsyncWaitHandle verwenden, um ein Synchronisationsobjekt abzurufen und darauf zu warten. Zum Beispiel:
' 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 ); |
Callback-Funktion beim Aufruf von BeginExecuteReader angeben Sie können beim Aufruf von BeginExecuteReader eine Callback-Funktion angeben. Zum Beispiel:
' 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 } |
Die Callback-Funktion wird in einem separaten Thread ausgeführt, sodass die üblichen Warnungen bezüglich der Aktualisierung der Benutzeroberfläche in einem Programm mit mehreren Threads gelten.
Kommentieren Sie diese Seite in DocCommentXchange. Senden Sie uns Feedback über diese Seite via E-Mail. |
Copyright © 2009, iAnywhere Solutions, Inc. - SQL Anywhere 11.0.1 |