SQL 文の非同期実行を終了します。
Visual Basic Public Function EndExecuteNonQuery( _ ByVal asyncResult As IAsyncResult _ ) As Integer
C# public int EndExecuteNonQuery( IAsyncResult asyncResult );
asyncResult BeginExecuteNonQuery への呼び出しによって返される System.IAsyncResult。
影響を受けたローの数 (ExecuteNonQuery と同じ動作)。
BeginExecuteNonQuery を呼び出すごとに、EndExecuteNonQuery を呼び出す必要があります。呼び出しは、BeginExecuteNonQuery が返されてから行います。ADO.NET はスレッドに対応していないため、各自で BeginExecuteNonQuery が返されたことを確認する必要があります。EndExecuteNonQuery に渡される System.IAsyncResult は、完了した BeginExecuteNonQuery 呼び出しから返されるものと同じでなければなりません。EndExecuteNonQuery を呼び出して、BeginExecuteReader への呼び出しを終了すると、エラーになります。逆についても同様です。
コマンドの実行中にエラーが発生すると、EndExecuteNonQuery が呼び出されるときに例外がスローされます。
実行の完了を待機するには、4 通りの方法があります。
Call EndExecuteNonQuery EndExecuteNonQuery を呼び出すと、コマンドが完了するまでブロックします。次に例を示します。
' Visual Basic Dim cmd As ULCommand = new ULCommand( _ "UPDATE Departments" _ + " SET DepartmentName = 'Engineering'" _ + " WHERE DepartmentID=100", _ conn _ ) Dim res As IAsyncResult res = _ cmd.BeginExecuteNonQuery() ' perform other work ' this will block until the command completes Dim rowCount As Integer = _ cmd.EndExecuteNonQuery( res ) // C# ULCommand cmd = new ULCommand( "UPDATE Departments" + " SET DepartmentName = 'Engineering'" + " WHERE DepartmentID=100", conn ); IAsyncResult res = cmd.BeginExecuteNonQuery(); // perform other work // this will block until the command completes int rowCount = cmd.EndExecuteNonQuery( res ); |
Poll the IsCompleted property of the IAsyncResult IAsyncResult の IsCompleted プロパティをポーリングできます。次に例を示します。
' Visual Basic Dim cmd As ULCommand = new ULCommand( _ "UPDATE Departments" _ + " SET DepartmentName = 'Engineering'" _ + " WHERE DepartmentID=100", _ conn _ ) Dim res As IAsyncResult res = _ cmd.BeginExecuteNonQuery() While( !res.IsCompleted ) ' do other work End While ' this will block until the command completes Dim rowCount As Integer = _ cmd.EndExecuteNonQuery( res ) // C# ULCommand cmd = new ULCommand( "UPDATE Departments" + " SET DepartmentName = 'Engineering'" + " WHERE DepartmentID=100", conn ); IAsyncResult res = cmd.BeginExecuteNonQuery(); while( !res.IsCompleted ) { // do other work } // this will block until the command completes int rowCount = cmd.EndExecuteNonQuery( res ); |
Use the IAsyncResult.AsyncWaitHandle property to get a synchronization object IAsyncResult.AsyncWaitHandle プロパティを使用して同期オブジェクトを取得し、その状態で待機できます。次に例を示します。
' Visual Basic Dim cmd As ULCommand = new ULCommand( _ "UPDATE Departments" _ + " SET DepartmentName = 'Engineering'" _ + " WHERE DepartmentID=100", _ conn _ ) Dim res As IAsyncResult res = _ cmd.BeginExecuteNonQuery() ' perform other work Dim wh As WaitHandle = res.AsyncWaitHandle wh.WaitOne() ' this will not block because the command is finished Dim rowCount As Integer = _ cmd.EndExecuteNonQuery( res ) // C# ULCommand cmd = new ULCommand( "UPDATE Departments" + " SET DepartmentName = 'Engineering'" + " WHERE DepartmentID=100", conn ); IAsyncResult res = cmd.BeginExecuteNonQuery(); // perform other work WaitHandle wh = res.AsyncWaitHandle; wh.WaitOne(); // this will not block because the command is finished int rowCount = cmd.EndExecuteNonQuery( res ); |
Specify a callback function when calling BeginExecuteNonQuery BeginExecuteNonQuery の呼び出し時にコールバック関数を指定できます。次に例を示します。
' 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 rowCount As Integer = _ cmd.EndExecuteNonQuery( res ) End Sub |
' elsewhere in the code Private Sub DoStuff() Dim cmd As ULCommand = new ULCommand( _ "UPDATE Departments" _ + " SET DepartmentName = 'Engineering'" _ + " WHERE DepartmentID=100", _ conn _ ) Dim res As IAsyncResult = _ cmd.BeginExecuteNonQuery( _ 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 int rowCount = cmd.EndExecuteNonQuery(); } |
// elsewhere in the code private void DoStuff() { ULCommand cmd = new ULCommand( "UPDATE Departments" + " SET DepartmentName = 'Engineering'" + " WHERE DepartmentID=100", conn ); IAsyncResult res = cmd.BeginExecuteNonQuery( callbackFunction, cmd ); // perform other work. The callback function // will be called when the command completes } |
コールバック関数は別のスレッドで実行するため、スレッド化されたプログラム内でのユーザ・インタフェースの更新に関する通常の注意が適用されます。
Copyright © 2009, iAnywhere Solutions, Inc. - SQL Anywhere 11.0.1 |