Click here to Skip to main content
15,916,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can someone help me figure out why my initial UI test message is not displaying correctly when I use async and await keywords in VS 2015?
My code follows here:

C#
private async Task<datatable> ExecuteSQLASync(SQLiteConnection conn, string sqlStmnt)
{
   SQLiteDataAdapter da = new SQLiteDataAdapter(sqlStmnt, conn);
   DataSet ds = new DataSet();
   da.Fill(ds);

   Thread.Sleep(2000); // only to slow things down for testing.

   return ds.Tables[0];
}

async private void StartExecuteSQLAsync(SQLiteConnection conn, string sqlStmnt)
{
   try
   {
      txtMsg.Text = "Starting SQL Query in Background: " + sqlStmnt;
      txtMsg.ForeColor = Color.Blue;

      DataTable dt = await ExecuteSQLASync(conn, sqlStmnt);

      dataGridViewSQLQuery.DataSource = dt;
      lblNumRowsText.Text = m_SQLQueryDataTable.Rows.Count.ToString();

      txtMsg.Text = "Results for SQL query:" + sqlStmnt;
      txtMsg.ForeColor = Color.Green;
   }
   catch (Exception ex)
   {
      txtMsg.Text = ex.Message;
      txtMsg.ForeColor = Color.Red;
   }
}


The problem is, I never see my first message in blue - I see no message at all until after the ExecuteSQLASync method has completed and then I see the final message in green. What am I missing?

Thanks.

What I have tried:

I have tried using the async/await keywords to create a non-blocking asynchronous database access but I must have misunderstood the correct usage.
Posted
Updated 14-Apr-16 11:03am
v3

1 solution

you are using the async / await keywords but you don't seem to start a new thread that runs in the background, I would give this a try:

C#
private DataTable ExecuteSQLASync(SQLiteConnection conn, string sqlStmnt)
{
  // ...
}

DataTable dt =  await Task.Run(   () => ExecuteSQLASync(conn, sqlStmnt)   );
 
Share this answer
 
Comments
Model Sounds Inc. 14-Apr-16 18:16pm    
Thank you so much. That was the solution.
FranzBe 15-Apr-16 2:32am    
Good to hear; perhaps you should mark your question as solved, so that it gets the correct status in the question list;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900