Running async method on Load

Asked

Viewed 92 times

-1

I have a method void that performs a procedure in the database and has no return, call this method in the load of the form, however when the procedure delays the program.

How do I pass this method to async not to curb implementation?

  private  void AccountsReceivable() 
  {
            using  (SqlConnection con = clsdb.AbreBanco())
            {
                using (SqlCommand cmd1 = new SqlCommand("procedimento", con))
                {
                    cmd1.CommandType = CommandType.StoredProcedure;
                    cmd1.CommandTimeout = 20000;
                    cmd1.ExecuteNonQuery();
                }
            }
   }


private void Form1_Load(object sender, EventArgs e)
{
  Task Receivable  = new Task(AccountsReceivable);
  Receivable.Start();
}
  • if you change the method to async, "Form_load" should also be, otherwise you will need to use a Wait, which will force the other method to be synchronous... you can start a new task and let it run without blocking the processing. You can do this for example by using Task.Run

  • @Ricardopunctual Can you give me an example using the code above, I didn’t understand the part about creating a new task and let run without blocking.

  • @mba this method that you run in the database you say is time consuming and that why you "lock" your program, is that if you improve the query, as for example in a Select with WITH command (NOLOCK) which indicates that it will not be necessary to lock the table while reading the data, would that help?? ex: "SELECT COUNT(Name) FROM Tabelapessoas WITH (NOLOCK) "

1 answer

1

Before making asynchronous you should fix a serious problem that is this try-catch doing nothing.

I have no idea what you’re trying to do with this Task in Form1_Load(), but remove this, everything must be solved in the other method, this if really the query has taken too long, otherwise it does not compensate.

It would be something like that (at least a naive implementation, not knowing what it really needs):

private async Task AccountsReceivable() {
    using  (SqlConnection con = clsdb.AbreBanco())
    using (var cmd1 = new SqlCommand("procedimento", con)) {
        cmd1.CommandType = CommandType.StoredProcedure;
        cmd1.CommandTimeout = 20000;
        await cmd1.ExecuteNonQueryAsync().ConfigureAwait(false);
    }
}

I put in the Github for future reference.

You can read in more detail and adapt to your needs.

Documentation.

  • This in the load to be executed automatically without user action. So it is running automatically? The try-catch in the original code has log who informs if there was any exception, I thought it would not be relevant in the question.

  • If it’s not relevant take it off. I don’t know what you mean by automatic, it will run. I don’t even know if it’s what you want, it’s just what solves the problem you reported. Asynchronous code needs to be done right for what it wants, and requires understanding of how it works to meet exact demand.

  • I edited the question without it. Automatic what I mean is that as soon as you open the form the method will be executed without needing an event click. That’s why I was calling on load.

  • Will be if you call this method.

  • But when I call it that Task Receivable = new Task(AccountsReceivable);
 Receivable.Start(); he tells me he has the kind of incorrect feedback.

  • But it’s called the normal method, as I wrote, I don’t know what you did there, throw it away, don’t invent things you don’t understand.

  • Edit your answer, to work this way I have to put the load as async and call the method using await AccountsReceivable(); Note: "don’t invent things you don’t understand" this type of comment is unnecessary in the topic and forum.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.