There is such a method
async Task<DataTable> TestAsync(string mySelectQuery, DB2Connection myConnection) { Task<DataTable> us = Task.Factory.StartNew(() => { using (DB2Command myCommand = new DB2Command(mySelectQuery, myConnection)) { using (DbDataReader myReader = myCommand.ExecuteReader()) { DataTable dt = new DataTable(); dt.Load(myReader); return dt; } } }); return await us; }
I call this:
IEnumerable<Task<DataTable>> tasks = from unds in new [] { "select * from test1", "select * from test2" } select TestAsync(unds, myConnection); DataTable[] uds = await Task.WhenAll(tasks);
And an exception is thrown.
Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints
Someone can explain to me why in this case DataTable
gives an error? is it local, and with asyncry, the method is copied and all its local variables too? But if you do like this, then there is no error!
lock (ob) { DataTable dt = new DataTable(); dt.Load(myReader); return dt; }
return await
is stupid. From your code, you can remove the wordsasync
andawait
without losing meaning. - Pavel Mayorov