Asynchronous database callback
-
I have a small problem with retrieving a DataTable object from an asynchronous call. My setup: I have a Winfom app that has a reference to a library. In the Library I have a class called Run which does:
public static DataTable GetAsyncDataTable(String Query, CommandType sqlCommandType) { Abstract.AbstractSqlCommand mySqlCommand = new Concrete.ConcreteSqlCommand(); mySqlCommand.GetAsyncDataTableBegin(Query, sqlCommandType); queryDuration = mySqlCommand.queryDuration; return mySqlCommand.MyTable; }
In the GetAsyncDataTableBegin method the code is
public override IAsyncResult GetAsyncDataTableBegin(String Query, CommandType sqlCommandType) { return GetAsyncDataTableBegin(Query, null, sqlCommandType, null); } public override IAsyncResult GetAsyncDataTableBegin(String Query, List<SqlParameter> sqlParameters, CommandType sqlCommandType, String ConnectionString) { DateTime nu = DateTime.Now; SqlCommand command = Get(Query, sqlParameters, sqlCommandType, ConnectionString); return command.BeginExecuteReader(GetAsyncDataTableEnd, command, CommandBehavior.CloseConnection); }
In the GetAsyncDataTableEnd Method the code is
public override void GetAsyncDataTableEnd(IAsyncResult myResult) { SqlCommand command = null; SqlDataReader dr = null; DateTime nu = DateTime.Now; DataTable myTable = new DataTable(); try { command = (SqlCommand)myResult.AsyncState; dr = command.EndExecuteReader(myResult); myTable.Load(dr); MyTable = myTable; //MyTable is a public override DataTable MyTable (property) } catch (Exception err) { String poef = err.ToString(); } finally { if (null != dr) dr.Close(); command.Connection.Close(); DateTime klaar = DateTime.Now; queryDuration = (klaar - nu).Duration(); string Time = queryDuration.ToString(); } }
And now my problem: When I call the Run.GetAsyncDataTable (first code block), my data is retrieved asynchronously in another thread but the lines in Run.GetAsyncDataT
-
I have a small problem with retrieving a DataTable object from an asynchronous call. My setup: I have a Winfom app that has a reference to a library. In the Library I have a class called Run which does:
public static DataTable GetAsyncDataTable(String Query, CommandType sqlCommandType) { Abstract.AbstractSqlCommand mySqlCommand = new Concrete.ConcreteSqlCommand(); mySqlCommand.GetAsyncDataTableBegin(Query, sqlCommandType); queryDuration = mySqlCommand.queryDuration; return mySqlCommand.MyTable; }
In the GetAsyncDataTableBegin method the code is
public override IAsyncResult GetAsyncDataTableBegin(String Query, CommandType sqlCommandType) { return GetAsyncDataTableBegin(Query, null, sqlCommandType, null); } public override IAsyncResult GetAsyncDataTableBegin(String Query, List<SqlParameter> sqlParameters, CommandType sqlCommandType, String ConnectionString) { DateTime nu = DateTime.Now; SqlCommand command = Get(Query, sqlParameters, sqlCommandType, ConnectionString); return command.BeginExecuteReader(GetAsyncDataTableEnd, command, CommandBehavior.CloseConnection); }
In the GetAsyncDataTableEnd Method the code is
public override void GetAsyncDataTableEnd(IAsyncResult myResult) { SqlCommand command = null; SqlDataReader dr = null; DateTime nu = DateTime.Now; DataTable myTable = new DataTable(); try { command = (SqlCommand)myResult.AsyncState; dr = command.EndExecuteReader(myResult); myTable.Load(dr); MyTable = myTable; //MyTable is a public override DataTable MyTable (property) } catch (Exception err) { String poef = err.ToString(); } finally { if (null != dr) dr.Close(); command.Connection.Close(); DateTime klaar = DateTime.Now; queryDuration = (klaar - nu).Duration(); string Time = queryDuration.ToString(); } }
And now my problem: When I call the Run.GetAsyncDataTable (first code block), my data is retrieved asynchronously in another thread but the lines in Run.GetAsyncDataT
Um ... do you understand what 'asynchronous' means? This is exactly what is supposed to happen; to retrieve the result from an asynchronous call you should provide a callback delegate, which is executed some time later, and in the meantime, your code continues to run. If you want the result to be available 'immediately' on the next line of code, why not use a synchronous query call?
-
Um ... do you understand what 'asynchronous' means? This is exactly what is supposed to happen; to retrieve the result from an asynchronous call you should provide a callback delegate, which is executed some time later, and in the meantime, your code continues to run. If you want the result to be available 'immediately' on the next line of code, why not use a synchronous query call?
Hey BobJanova, I do not want to run an synchronous call to the database because I have sometimes a report running for over more than 1 hour. Maybe my Run Class should be omitted and my form should use the Abstract/Concrete classes directly
In Word you can only store 2 bytes. That is why I use Writer.
-
Hey BobJanova, I do not want to run an synchronous call to the database because I have sometimes a report running for over more than 1 hour. Maybe my Run Class should be omitted and my form should use the Abstract/Concrete classes directly
In Word you can only store 2 bytes. That is why I use Writer.
-
If you have a report running for an hour you definitely don't want to be waiting for it to return! Yes, using the asynchronous database classes directly, and passing a callback to be called when the report is complete, might be a better way forward.
But in which class do I define the delegate?
In Word you can only store 2 bytes. That is why I use Writer.
-
But in which class do I define the delegate?
In Word you can only store 2 bytes. That is why I use Writer.
-
No in the Concrete classes
In Word you can only store 2 bytes. That is why I use Writer.