SAConnection conn = new SAConnection("DSN=SQL Anywhere 12 Demo");
conn.Open();
SACommand cmd = new SACommand( "SELECT * FROM Departments", conn );
IAsyncResult res = cmd.BeginExecuteReader();
// perform other work
// this blocks until the command completes
SADataReader reader = cmd.EndExecuteReader( res );
(2) IAsyncResult の IsCompleted プロパティをポーリングする。
IAsyncResult の IsCompleted プロパティをポーリングできます。例:
SAConnection conn = new SAConnection("DSN=SQL Anywhere 12 Demo");
conn.Open();
SACommand cmd = new SACommand( "SELECT * FROM Departments", conn );
IAsyncResult res = cmd.BeginExecuteReader();
while( !res.IsCompleted ) {
// do other work
}
// this does not block because the command is finished
SADataReader reader = cmd.EndExecuteReader( res );
SAConnection conn = new SAConnection("DSN=SQL Anywhere 12 Demo");
conn.Open();
SACommand cmd = new SACommand( "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
SADataReader reader = cmd.EndExecuteReader( res );
(4) BeginExecuteReader の呼び出し時にコールバック関数を指定する。
BeginExecuteReader の呼び出し時にコールバック関数を指定できます。例:
private void callbackFunction( IAsyncResult ar ) {
SACommand cmd = (SACommand) ar.AsyncState;
// this does not block since the command has completed
SADataReader reader = cmd.EndExecuteReader();
}
// elsewhere in the code
private void DoStuff() {
SAConnection conn = new SAConnection("DSN=SQL Anywhere 12 Demo");
conn.Open();
SACommand cmd = new SACommand( "SELECT * FROM Departments", conn );
IAsyncResult res = cmd.BeginExecuteReader( callbackFunction, cmd );
// perform other work. The callback function will be
// called when the command completes
}