Click here to Skip to main content
15,868,141 members
Home / Discussions / C#
   

C#

 
AnswerRe: How to get Millions of files from folder and bulk insert in database Pin
Michael Sydney Balloni16-Oct-21 17:36
professionalMichael Sydney Balloni16-Oct-21 17:36 
AnswerRe: How to get Millions of files from folder and bulk insert in database Pin
jschell24-Oct-21 6:50
jschell24-Oct-21 6:50 
QuestionPredicateBuilder Question Pin
Kevin Marois15-Oct-21 5:24
professionalKevin Marois15-Oct-21 5:24 
AnswerRe: PredicateBuilder Question Pin
Eddy Vluggen15-Oct-21 14:44
professionalEddy Vluggen15-Oct-21 14:44 
QuestionTask.WhenAll ConfigureAwait Question Pin
Kevin Marois14-Oct-21 12:57
professionalKevin Marois14-Oct-21 12:57 
AnswerRe: Task.WhenAll ConfigureAwait Question Pin
Richard Deeming14-Oct-21 23:15
mveRichard Deeming14-Oct-21 23:15 
GeneralRe: Task.WhenAll ConfigureAwait Question Pin
Kevin Marois15-Oct-21 4:53
professionalKevin Marois15-Oct-21 4:53 
GeneralRe: Task.WhenAll ConfigureAwait Question Pin
Richard Deeming15-Oct-21 5:59
mveRichard Deeming15-Oct-21 5:59 
Kevin Marois wrote:
What's a 'closure'?
When an anonymous function / lambda method references local variables, those variables are hoisted into a compiler-generated class called a closure.

Given:
C#
public void Foo()
{
    List<ProjectHeaderEntity> projects = null;
    await Task.Run(() => { projects = AppCore.BizObject.GetProjectHeaders(AppCore.AppCompany.Id).ToList(); });
    Console.WriteLine(projects.Count);
}
the compiler will generate something closer to:
C#
private sealed class <>_SomeRandomGeneratedName
{
    public List<ProjectHeaderEntity> projects;
    public Action TheAction;
    public void TheActualMethod()
    {
        this.projects = AppCore.BizObject.GetProjectHeaders(AppCore.AppCompany.Id).ToList();
    }
}

public void Foo()
{
    var closure = new <>_SomeRandomGeneratedName();
    closure.TheAction = new Action(closure.TheActualMethod);
    await Task.Run(closure.TheAction);
    Console.WriteLine(closure.projects.Count);
}

By changing the code so that it no longer refers to the local variables, you can get rid of the closure class:
C#
public void Foo()
{
    List<ProjectHeaderEntity> projects = await Task.Run(() => AppCore.BizObject.GetProjectHeaders(AppCore.AppCompany.Id).ToList());
    Console.WriteLine(projects.Count);
}
should compile to something like:
C#
private static Func<List<ProjectHeaderItem>> TheCachedDelegate;

private static List<ProjectHeaderEntity> TheActualMethod()
{
    return AppCore.BizObject.GetProjectHeaders(AppCore.AppCompany.Id).ToList();
}

public void Foo()
{
    if (TheCachedDelegate == null)
    {
        TheCachedDelegate = new Func<List<ProjectHeaderItem>>(TheActualMethod);
    }
    
    List<ProjectHeaderEntity> projects = await Task.Run(TheCachedDelegate);
    Console.WriteLine(projects.Count);
}
which has significantly fewer allocations, particularly when called multiple times.


Kevin Marois wrote:
I thought that WITHOUT WhenAll, the first would run, then the other one.
The task starts running as soon as you call Task.Run. Your code waits for the task to complete when you await it. If you separate the two, you can do other things in between the task starting and waiting for the task to complete, including starting other tasks.
C#
await Task.Run(() => ...); // Waits for this one to finish...
await Task.Run(() => ...); // then runs this one and waits for it to finish...
DoSomething(); // then does this.
C#
Task a = Task.Run(() => ...); // Starts this one...
Task b = Task.Run(() => ...); // then starts this one...
await a; // then waits for the first one to finish...
await b; // then waits for the second one to finish...
DoSomething(); // then does this.
C#
Task a = Task.Run(() => ...); // Starts this one...
Task b = Task.Run(() => ...); // then starts this one...
await Task.WhenAll(new[] { a, b }); // then waits for both tasks to finish...
DoSomething(); // then does this.

You generally need Task.WhenAll when you have an unknown number of tasks to wait for. With two tasks, where you want the return value, and they are returning different types, it's easier to just await each in turn. If you used Task.WhenAll, you'd either have to await the tasks again, or use their Result property, to get the return value from each.



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer

GeneralRe: Task.WhenAll ConfigureAwait Question Pin
Kevin Marois15-Oct-21 6:29
professionalKevin Marois15-Oct-21 6:29 
GeneralRe: Task.WhenAll ConfigureAwait Question Pin
Kevin Marois15-Oct-21 8:08
professionalKevin Marois15-Oct-21 8:08 
GeneralRe: Task.WhenAll ConfigureAwait Question Pin
Richard Deeming17-Oct-21 21:58
mveRichard Deeming17-Oct-21 21:58 
GeneralRe: Task.WhenAll ConfigureAwait Question Pin
Kevin Marois18-Oct-21 4:01
professionalKevin Marois18-Oct-21 4:01 
QuestionCodility fails to compile even though it compiles in VS code? Pin
Cliff ludo13-Oct-21 6:41
Cliff ludo13-Oct-21 6:41 
AnswerRe: Codility fails to compile even though it compiles in VS code? Pin
BillWoodruff13-Oct-21 17:11
professionalBillWoodruff13-Oct-21 17:11 
Generalpass array to function Pin
Cliff ludo12-Oct-21 23:04
Cliff ludo12-Oct-21 23:04 
GeneralRe: pass array to function Pin
Richard Deeming12-Oct-21 23:25
mveRichard Deeming12-Oct-21 23:25 
GeneralRe: pass array to function Pin
Cliff ludo12-Oct-21 23:32
Cliff ludo12-Oct-21 23:32 
QuestionCreating An Async Method Pin
Kevin Marois7-Oct-21 11:02
professionalKevin Marois7-Oct-21 11:02 
AnswerRe: Creating An Async Method Pin
Richard Deeming7-Oct-21 21:41
mveRichard Deeming7-Oct-21 21:41 
GeneralRe: Creating An Async Method Pin
Kevin Marois8-Oct-21 6:00
professionalKevin Marois8-Oct-21 6:00 
GeneralRe: Creating An Async Method Pin
Richard Deeming8-Oct-21 6:35
mveRichard Deeming8-Oct-21 6:35 
GeneralRe: Creating An Async Method Pin
Kevin Marois8-Oct-21 6:38
professionalKevin Marois8-Oct-21 6:38 
GeneralRe: Creating An Async Method Pin
Kevin Marois8-Oct-21 9:46
professionalKevin Marois8-Oct-21 9:46 
GeneralRe: Creating An Async Method Pin
lmoelleb9-Oct-21 0:18
lmoelleb9-Oct-21 0:18 
GeneralRe: Creating An Async Method Pin
Kevin Marois9-Oct-21 7:03
professionalKevin Marois9-Oct-21 7:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.