0

I just wanted to create a dummy ASP.NET MVC project where i wanted to explore more about async & await.I created async methods in DB access Layer and Repository Layer and i also make sure that the action is also written in async way. I noticed that a single method takes around 7ms to execute, so logically if i call the method 3 times i also should take 7 ms or around 7ms ! But its taking about 20-23 ms. I am sure that i am doing something terribly wrong. My Code snippet goes below:

DATA ACCESS LAYER:

    public async Task<DataTable> DtGetAllCustomers()
    {
        await Task.Run(() =>
        {
            _dtCustomer = new DataTable();
            _connectionString = Repo.GetConnString();
            _spName = "spGetCus";
            _spParameters = new SqlParameter[]
            {
            new SqlParameter("@QryOption",1)
            };
            using (var conn = new SqlConnection(_connectionString))
            {
                using (var cmd = new SqlCommand())
                {
                    cmd.Connection = conn;
                    cmd.CommandText = _spName;
                    cmd.Parameters.AddRange(_spParameters);
                    cmd.CommandType = CommandType.StoredProcedure;
                    conn.Open();

                    using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                    {
                        da.Fill(_dtCustomer);
                        conn.Close();
                        da.Dispose();
                    }
                }
            }

        });

        return _dtCustomer;
    }

REPOSITORY:

 public async Task<List<CustomerModel>> GetAllCustomers()
            {
                _dtCustomer = await _customerDal.DtGetAllCustomers();
                List<CustomerModel> allCustomers = new List<CustomerModel>();
                return allCustomers = (from DataRow row in _dtCustomer.Rows
                                       select new CustomerModel
                                       {
                                           CustomerId = (int)row["CustomerId"],
                                           CustomerName = (string)row["CustomerName"]
                                       }).ToList();
            }

ACTION:

public async Task<ActionResult> Index()
        {
            var watch = System.Diagnostics.Stopwatch.StartNew();
            List<CustomerModel> model = new List<CustomerModel>();
            CustomerRepo2 aa = new CustomerRepo2();
            await aa.GetAllCustomers();
            await aa.GetAllCustomers();
            await aa.GetAllCustomers();
            watch.Stop();
            var elapsedMs = watch.ElapsedMilliseconds;
            ViewBag.time = elapsedMs;
            return View();
        }
11
  • What makes you think that calling 3 methods with await will execute them in parallel? Parallel programming and asynchronous programming are different concepts. You should get familiar with the theory before writing code Commented Oct 22, 2018 at 11:20
  • 3
    Besides that, wrapping a method with Task.Run is considered a bad practice. Moreover, creating Thread Pool threads to do I/O operations is a waste of processing power. Look at this answer to see how you can do that query in a truly asynchronous way Commented Oct 22, 2018 at 11:21
  • 1
    As a last comment, you may want to look into an ORM instead of loading a DataTable to then get your entities Commented Oct 22, 2018 at 11:23
  • @CamiloTerevinto I know the basic difference ! neither i am expecting "Parallel" performance nor exactly 7 ms ! Please read my question carefully, i mentioned "should take 7 ms or around 7ms" ... so it may 10 or 13 or any near value . is it logical to take 3 times of single execution ! thats why the question raised in my mind :) Commented Oct 22, 2018 at 11:34
  • 1
    That's because of the Task.Delay being an asynchronous operation. As I mentioned in my second comment, you are wrapping synchronous operations in a Task, which is a waste. Commented Oct 22, 2018 at 11:43

1 Answer 1

2

You're awaiting each call, so before it can move onto the next, it will pause execution. Consider creating three tasks, and awaiting them all with Task.WhenAll():

var task1 = aa.GetAllCustomers();
var task2 = aa.GetAllCustomers();
var task2 = aa.GetAllCustomers();
await Task.WhenAll(task1, task2, task3);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.