markokstate
markokstate

Reputation: 961

Async DataTable Call

Here is what I have so far... FillDataTable() works just fine but I'm running into issues because I have synchronous code and lots of calls to make back to back. Many of them are ready to run before the proceeding one finishes. Trying to convert this to async/await. I copied FillAsync() from other examples I had seen, so it might be Franken-code.

public static DataTable FillDataTable(string sql, Database _db) {
           using (OleDbConnection conn = NewConnectionFromGivenDB(_db)) {
                  conn.Open();
                  using (OleDbCommand comm = new OleDbCommand(sql, conn)) {
                        using (OleDbDataAdapter da = new OleDbDataAdapter(comm)) {
                                using (DataTable dt = new DataTable()) {
                                    da.Fill(dt);
                                }
                                conn.Close();
                                return dt;
                         }
                   }
           }
    }

    public static async Task<DataTable> FillAsync(string sql, Database _db) {
           return await Task.Run(() => { return FillDataTable(sql, _db); });
    }

When I call the following in an async method, the code never returns.

DataTable dt = await FillAsync(sql, Database.Oracle);

Can anyone see what is wrong or give me some suggestions for a better way forward?

Upvotes: 0

Views: 1142

Answers (1)

usr
usr

Reputation: 171178

You are calling Result or Wait on some task in code not shown here. This causes the classic ASP.NET (or WinForms/WPF) deadlock. Use await for all waits.

Many of them are ready to run before the proceeding one finishes

Are you trying to run things in parallel? Your code does not do that. await does not start tasks; it waits for tasks that are already running. It ends parallelism.

Upvotes: 2

Related Questions