Loading only appears at the end of code execution

Viewed 182

I have a button that erases records from a sqlite table. I tried to put loading screen but it just appears at the end. Can you help me?

btnImportar.Click += delegate
{
    UserDialogs.Instance.ShowLoading("Aguarde...", MaskType.Black);

    string stm = "DELETE FROM Trender";

    using (SqliteCommand cmd2 = new SqliteCommand(stm, con))
    {
        cmd2.ExecuteNonQuery();
    }
}
4 Answers

you could try this:

btnImportar.Click += delegate
 {
  UserDialogs.Instance.ShowLoading("Aguarde...", MaskType.Black);
  Task.Run(()=> {
       string stm = "DELETE FROM Trender";
       using (SqliteCommand cmd2 = new SqliteCommand(stm, con))
         {
          cmd2.ExecuteNonQuery();
         }
      });

 }

I think you are holding back the UI thread from updating. Do something like,

UserDialogs.Instance.ShowLoading("Aguarde...", MaskType.Black);

ThreadPool.QueueUserWorkItem(_ => {
    string stm = "DELETE FROM Trender";

    using (SqliteCommand cmd2 = new SqliteCommand(stm, con))
    {
        cmd2.ExecuteNonQuery();
    }

    Activity.RunOnUiThread(() => {
        // Dismiss the dialog
    })
})

Can you try something like the following:

btnImportar.Click += button_Click;

private async void button_Click(object sender, EventArgs e)
{ 
    UserDialogs.Instance.ShowLoading("Aguarde...", MaskType.Black);

    await DeleteContent();
}

private async Task DeleteContent()
{
    string stm = "DELETE FROM Trender";
    using (SqliteCommand cmd2 = new SqliteCommand(stm, con))
    {
        await cmd2.ExecuteNonQueryAsync();
    }
}
btnImportar.Click += delegate
 {
UserDialogs.Instance.ShowLoading("Aguarde...", MaskType.Black);
 Task.Factory.StartNew(async () =>
            {
string stm = "DELETE FROM Trender";

using (SqliteCommand cmd2 = new SqliteCommand(stm, con))
{
   await  cmd2.ExecuteNonQueryAsync();
} 
 });
}
Related