I have this code in a method:
Task<int> linksCount = Task<int>.Factory.StartNew(() => { return DatabaseIn.GetUrlsCount(); });
Task<int> imagesCount = Task<int>.Factory.StartNew(() => { return DatabaseIn.GetImagesCount(); });
Later the linksCount
and imagesCount.Result
is displayed. However, when I run this code the first time. I get the following exception:
There is already an open DataReader associated with this Connection which must be closed first.
The code where the exception is thrown is:
MySqlCommand comm = null;
string cmdString = "SELECT COUNT(*) FROM damocles.imagestoassess;";
comm = new MySqlCommand(cmdString, conn);
if (comm.ExecuteScalar() == null)
{
comm.Dispose();
conn.Close();
return 0;
}
int res = Convert.ToInt32(comm.ExecuteScalar());
comm.Dispose();
conn.Close();
return res;
The exception is thrown on the first call to ExecuteScalar
. If I enclose the code in a try..catch
clause the code continues but it throws another error in another part of the code.
Question:
- Why does it throw the exception? It doesn't throw any exception when run synchronously.
- How do I fix the code to work Asynchronously?
Thanks.