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 8558565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:50:28+00:00 2026-06-11T15:50:28+00:00

I’m trying to implement multi-threading as suggested here: Spawn Multiple Threads for work then

  • 0

I’m trying to implement multi-threading as suggested here: Spawn Multiple Threads for work then wait until all finished

Code looks like so:

var resetEvent = new ManualResetEvent(false);
            var clientsCount = IDLocal.UserSessions.Count;

            // Load sessions:
            IDLocal.UserSessions = new IDSessions();

            // Start thread for each client 
            foreach (IDSession session in IDLocal.UserSessions)
            {
                var clnt = session;
                new Thread(
                    delegate()
                    {
                        Debug.WriteLine(Thread.CurrentThread.ManagedThreadId);
                        clnt.FailedToRespond = !this.SendDirect(data, this.GetHostAddress(clnt.HostName), clnt.PortNumber);

                        // If we're the last thread, signal 
                        if (Interlocked.Decrement(ref clientsCount) == 0) resetEvent.Set();
                    })
                    .Start();
            }

Here I’m getting ReSharper warning: if (Interlocked.Decrement(ref clientsCount) == 0)

It suggests that I’m accessing modified closure (clientsCount)

Is that a valid suggestion?

  • 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-11T15:50:29+00:00Added an answer on June 11, 2026 at 3:50 pm

    The warning is meant to cover code such as

    int i = 1;
    Func<int> f = () => i + 1;
    i = 2;
    int j = f(); // What does this set j to?
    

    where the updated value of i will be used when calling f. The warning suggests to change this to

    int i = 1;
    int icopy = i;
    Func<int> f = () => icopy + 1;
    i = 2;
    int j = f(); // Now what does this set j to?
    

    if you want f to see the value of i as it was when the delegate was created. In the context of foreach loops, it is very easy to otherwise get unintuitive behaviour. There has been a change in the meaning of foreach loops in C# 5 because of exactly this.

    Since that’s not what you want here, since you do want to see changes to the captured variable, don’t change anything in the code.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
I have a text area in my form which accepts all possible characters from
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka

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.