I am just learning to use asynchronous ADO commands and I would like to wait on the BeginExecuteRea der() IAsyncResult wait handle.
When I use
the main thread does not wait it just continues executing and eventually terminates killing the reader thread.
I my code is as follows
thank you for the help
john
When I use
Code:
WaitHandle waitHandle = asyncresult.AsyncWaitHandle; waitHandle.WaitOne();
I my code is as follows
Code:
static void Main( string[] args ) {
dbRecordsHolder = new ArrayList();
SqlConnection connection = new SqlConnection( connectionString );
SqlCommand command = new SqlCommand( commandString, connection );
connection.Open();
AsyncCallback callback = new AsyncCallback( DataReaderIsReady );
IAsyncResult asyncresult = command.BeginExecuteReader( callback, command );
WaitHandle waitHandle = asyncresult.AsyncWaitHandle;
waitHandle.WaitOne();
Console.WriteLine( "Done" );
}
private static void DataReaderIsReady( IAsyncResult result ) {
MessageBox.Show( "Results Load Complete", "I'm Done" );
SqlCommand command = (SqlCommand)result.AsyncState;
SqlDataReader sqlDr = command.EndExecuteReader( result );
if ( sqlDr.HasRows ) {
foreach ( DbDataRecord rec in sqlDr ) {
dbRecordsHolder.Add( rec );
}
}
Console.WriteLine( "Records Added" );
sqlDr.Close();
command.Connection.Dispose();
}
}
john