使用Task <int>从MySql读取数据时,DataReader已打开

我在方法中有此代码:

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:

已经有一个与此连接相关联的打开的DataReader,必须先关闭它。

引发异常的代码是:

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.

题:

  1. 为什么会引发异常?同步运行时不会引发任何异常。
  2. 如何修复代码以使其异步工作?

谢谢。