Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Why is the Task WhenAll blocking the UI thread? but not the awaits on every line? [closed]

When I run this code, there are no blocks, all fine and dandy:

    List<Category> cats = null;
    cats = await cat.GetAllAsync();//no blocks here
    cats = await cat.GetAllAsync();//no blocks here
    cats = await cat.GetAllAsync();//no blocks here
    cats = await cat.GetAllAsync();//no blocks here
    cats = await cat.GetAllAsync();//no blocks here

But this other code does and I don't get why, I appreciate your help,

    var tasks = new List<Task>();
    tasks.Add(cat.GetAllAsync());
    tasks.Add(cat.GetAllAsync());
    tasks.Add(cat.GetAllAsync());
    tasks.Add(cat.GetAllAsync());
    tasks.Add(cat.GetAllAsync());
    await Task.WhenAll(tasks); //this blocks the UI thread

EDIT: I have realized that the GetAllAsync executes the sproc asynchronously but then it creates a list of Categories from the DataSet it returns, but there is no async there, so the main thread picks that up and binds one hundred thousand categories! which blocks the UI! doh!

thanks for the help,

like image 654
Alex Avatar asked Dec 17 '25 12:12

Alex


1 Answers

Is cat.GetAllAsync re-entrant\thread safe? In your first example multiple calls to cat.GetAllAsync are run sequentially. In your second example potentially multiple calls to cat.GetAllAsync can run in parallel, my guess is that it causes a lock\deadlock inside cat.GetAllAsync

like image 129
shurik Avatar answered Dec 19 '25 02:12

shurik