Click here to Skip to main content
15,887,596 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: Closures (C#) Pin
Jeremy Falcon4-Mar-24 17:53
professionalJeremy Falcon4-Mar-24 17:53 
GeneralRe: Closures (C#) Pin
jmaida4-Mar-24 15:41
jmaida4-Mar-24 15:41 
GeneralRe: Closures (C#) Pin
Jeremy Falcon4-Mar-24 17:54
professionalJeremy Falcon4-Mar-24 17:54 
GeneralRe: Closures (C#) Pin
Jacquers4-Mar-24 17:43
Jacquers4-Mar-24 17:43 
GeneralRe: Closures (C#) Pin
englebart4-Mar-24 14:40
professionalenglebart4-Mar-24 14:40 
GeneralRe: Closures (C#) Pin
Peter Adam4-Mar-24 20:20
professionalPeter Adam4-Mar-24 20:20 
GeneralRe: Closures (C#) Pin
Fly Gheorghe4-Mar-24 21:36
Fly Gheorghe4-Mar-24 21:36 
GeneralRe: Closures (C#) Pin
RooN3y5-Mar-24 2:18
RooN3y5-Mar-24 2:18 
ChatGPT explained this to me. I feel bad that I couldn't figure this one out Frown | :(

Question to ChatGPT:
In this example c# code "i" can equal 11. can you fix it?

for (int i=0; i <= 10; i++)
{
    var task = Task.Run(async () =>
    {
      var convertResp = await StartTestConvert(i);
      ... other stuff
    });
}


Response:
Yes, in the provided code, the variable i is captured by the async lambda expression, which means by the time the async operations inside the lambda complete, the value of i may have changed. To fix this, you need to capture the loop variable correctly. Here's a way to do it:

for (int i = 0; i <= 10; i++)
{
    int current = i; // Capture the current value of i
    var task = Task.Run(async () =>
    {
        var convertResp = await StartTestConvert(current);
        // ... other stuff
    });
}

By capturing the loop variable inside the loop, you ensure that each async operation receives its own copy of the loop variable, preventing the issue where the value of i changes before the async operation completes.

GeneralRe: Closures (C#) Pin
Rich Shealer5-Mar-24 3:14
Rich Shealer5-Mar-24 3:14 
GeneralA Mouse Ring Pin
BernardIE53174-Mar-24 0:43
BernardIE53174-Mar-24 0:43 
GeneralRe: A Mouse Ring Pin
Kornfeld Eliyahu Peter4-Mar-24 1:05
professionalKornfeld Eliyahu Peter4-Mar-24 1:05 
GeneralRe: A Mouse Ring Pin
den2k884-Mar-24 3:17
professionalden2k884-Mar-24 3:17 
GeneralRe: A Mouse Ring Pin
dandy724-Mar-24 3:20
dandy724-Mar-24 3:20 
GeneralRe: A Mouse Ring Pin
Kornfeld Eliyahu Peter4-Mar-24 17:50
professionalKornfeld Eliyahu Peter4-Mar-24 17:50 
GeneralRe: A Mouse Ring Pin
mspad5-Mar-24 3:58
mspad5-Mar-24 3:58 
GeneralRe: A Mouse Ring Pin
dandy725-Mar-24 4:29
dandy725-Mar-24 4:29 
GeneralRe: A Mouse Ring Pin
mspad5-Mar-24 5:17
mspad5-Mar-24 5:17 
GeneralRe: A Mouse Ring Pin
PIEBALDconsult5-Mar-24 5:21
mvePIEBALDconsult5-Mar-24 5:21 
GeneralRe: A Mouse Ring Pin
mspad5-Mar-24 5:26
mspad5-Mar-24 5:26 
GeneralRe: A Mouse Ring Pin
dandy725-Mar-24 8:28
dandy725-Mar-24 8:28 
GeneralRe: A Mouse Ring Pin
MarkTJohnson4-Mar-24 3:28
professionalMarkTJohnson4-Mar-24 3:28 
GeneralRe: A Mouse Ring Pin
Gary Wheeler5-Mar-24 0:19
Gary Wheeler5-Mar-24 0:19 
GeneralRe: A Mouse Ring Pin
Jörgen Andersson4-Mar-24 4:15
professionalJörgen Andersson4-Mar-24 4:15 
GeneralRe: A Mouse Ring Pin
BernardIE53174-Mar-24 4:50
BernardIE53174-Mar-24 4:50 
GeneralRe: A Mouse Ring Pin
Marc Clifton4-Mar-24 6:36
mvaMarc Clifton4-Mar-24 6:36 

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.