0

I have a piece of code which basically looks like this:

using(var ctx = new myDbContext())
{
    var task1 = Task.Factory.StartNew(() => items = ctx.zsp_select_UserItems(UserId).ToList());
    var task2 = Task.Factory.StartNew(() => result = ctx.zsp_select_Transactions(UserId, intoPast, LastUpdatedAt).ToList());
    var taskList = new List<Task> { task1, task2 };
    Task.WaitAll(taskList.ToArray());
}

Basically what zsp_select_Transactions and zsp_select_UserItems are stored procedures which are mapped by ADO.NET Entity framework in my project. Since the two of these take a little bit longer to fetch back the results (due to pulling out large amount of data), I was wondering to trigger these at the same time, instead of waiting for the first procedure to finish it's execution and then triggering the second one...

My problem here with this piece of code is that it only works once ?? The second time I execute the code I get a following error:

The underlying provider failed to open

What could be causing this issue ? What is the most efficient way to trigger both of these procedures and execute them both at once, without second waiting for the first to finish?

Can someone help me out with this ?

User987
  • 3,663
  • 15
  • 54
  • 115

0 Answers0