Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8689725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:43:40+00:00 2026-06-12T23:43:40+00:00

Could someone explain me why this code is not working : … foreach (VisualChunk

  • 0

Could someone explain me why this code is not working :

...
foreach (VisualChunk chunk in SortedChunks)
{
    System.Action a = () => MyFunction_Threaded(chunk);
    Console.Write("Synchronous : ");
    a.Invoke();
    System.Threading.Tasks.Task.Factory.StartNew(
                                        a,
                                        CancellationToken.None,
                                        TaskCreationOptions.None,
                                        TaskScheduler.Default)
} 
...

private void MyFunction_Threaded(VisualChunk chunk)
{
        Console.WriteLine(chunk.ChunkID);
}

It gives me the following output at console :

Synchronous : 0
Synchronous : 1
Synchronous : 2
Synchronous : 3
Synchronous : 4
Synchronous : 4294967291
Synchronous : 4294967292
Synchronous : 4294967293
Synchronous : 4294967294
Synchronous : 4294967295
Synchronous : 4294967296
Synchronous : 4294967297
4294967297
4294967297
4294967297
4294967297
4294967297
4294967297
4294967297
4294967297
4294967297
4294967297
4294967297
Synchronous : 4294967298
4294967298

In fact this code is working perfectly when .net framework 4.5 is installed (win 8 or VS2012).
When only the 4.0 is installed, this problem is raising !

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T23:43:42+00:00Added an answer on June 12, 2026 at 11:43 pm

    Yes, as mentioned in comments above by @usr, the problem in your code is that you have “modified closure,” where the compiler is somewhat misinterpreting what you want to do when it builds the assembly language. It is a very poor design choice in the c# language. To get around it, you would need to make a copy of the variable in the loop right away:

    foreach (VisualChunk chunk in SortedChunks)
    {
      var chunkA = chunk;
      System.Action a = () => MyFunction_Threaded(chunkA);
      ...
    

    However, I would have to ask what the point of this is (i.e. what are you trying to accomplish with this code)?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Could someone explain why this line of code isn't working. No errors are given.
Could someone explain to me what this portion of code means? repeat scroll 0
Please could someone explain to me why this regex does not match anything, when
Could someone please explain what this code exactly means: for i,j in [(i,j) for
Could someone help explain this code to me? .text:00401270 ; int __cdecl main(int argc,const
Could someone explain to me why .next() is not working as I expect it
Could someone explain me why this piece of code randomly leaks LDAP connections? I
Could someone please explain this code, this is the same blog app explained on
Could someone explain me why this code: class safe_bool_base { //13 protected: typedef void
Could someone explain this (strange) behavior? Why is the length in the first example

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.