waitHandle.WaitOne() not waiting for command.BeginExcecuteReader()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • coltrane
    New Member
    • May 2010
    • 2

    #1

    waitHandle.WaitOne() not waiting for command.BeginExcecuteReader()

    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
    Code:
    WaitHandle waitHandle = asyncresult.AsyncWaitHandle;
    waitHandle.WaitOne();
    the main thread does not wait it just continues executing and eventually terminates killing the reader thread.

    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();
        }
      }
    thank you for the help

    john
Working...