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

  • Home
  • SEARCH
  • 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 8109707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T01:30:10+00:00 2026-06-06T01:30:10+00:00

I currently have a function that will create X tasks of a function based

  • 0

I currently have a function that will create X tasks of a function based on the amount of data and I would like to update an int variable with how many data each thread have executed to show a progressBar.

Currently all my UI is updated from a timer so instead of delegating from the threads to update the UI (which I believe would be a lot more trouble in this case) I will update a variable with the count that will later be picked by the timer and update the UI.

What I would like to know is, would a lock be a good way to update the variable or is there a better way ?

Will the timer be able to read that variable if it is being used very frequently or it would still be able to read it even if an older value while its being updated ?

Here is a roughly example:

private static readonly object counterLock = new object();
int myCounter = 0;

private void FunctionExecutedByAllRunnningThreads()
{
    int executed = 0;
    foreach (some data)
    {
        //do something with this data
        executed++;
    }
    lock (counterLock)
        myCounter += executed;
}
  • 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-06T01:30:12+00:00Added an answer on June 6, 2026 at 1:30 am

    What I would like to know is, would a lock be a good way to update the variable or is there a better way?

    If you’re just incrementing the value, you could use Interlocked.Increment to safely increment it without a lock.

    If you want to batch this, Interlocked.Add will let you add your thread-local total value to the counter in one shot, again without locking.

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

Sidebar

Related Questions

I'm currently trying to create a kernel module that will produce data based on
I'm currently developing a program that will generate reports based upon lead data. My
I am currently writing a MEX function that will have to work with a
I would like to have a function that takes a dictionary of string, array
I currently have a function that grabs the browser windows uri, parses out the
I currently have a Javascript function that uses a string to reference an object
I currently have a function written called saveWorkout that saves an NSMutableArray to another
I currently have the following MySQL query, which I would like to include in
I have a general create function that submits a new user to the database
I need to create a function that will run a query and return the

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.