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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:12:38+00:00 2026-05-17T17:12:38+00:00

I have a program that uses threads to perform time-consuming processes sequentially. I want

  • 0

I have a program that uses threads to perform time-consuming processes sequentially. I want to be able to monitor the progress of each thread similar to the way that the BackgroundWorker.ReportProgress/ProgressChanged model does. I can’t use ThreadPool or BackgroundWorker due to other constraints I’m under. What is the best way to allow/expose this functionality. Overload the Thread class and add a property/event? Another more-elegant solution?

  • 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-05-17T17:12:39+00:00Added an answer on May 17, 2026 at 5:12 pm

    Overload the Thread class and add a
    property/event?

    If by “overload” you actually mean inherit then no. The Thread is sealed so it cannot be inherited which means you will not be able to add any properties or events to it.

    Another more-elegant solution?

    Create a class that encapsulates the logic that will be executed by the thread. Add a property or event (or both) which can be used to obtain progress information from it.

    public class Worker
    {
      private Thread m_Thread = new Thread(Run);
    
      public event EventHandler<ProgressEventArgs> Progress;
    
      public void Start()
      {
        m_Thread.Start();
      }
    
      private void Run()
      {
        while (true)
        {
          // Do some work.
    
          OnProgress(new ProgressEventArgs(...));
    
          // Do some work.
        }
      }
    
      private void OnProgress(ProgressEventArgs args)
      {
    
        // Get a copy of the multicast delegate so that we can do the
        // null check and invocation safely. This works because delegates are
        // immutable. Remember to create a memory barrier so that a fresh read
        // of the delegate occurs everytime. This is done via a simple lock below.
        EventHandler<ProgressEventArgs> local;
        lock (this)
        {
          var local = Progress;
        }
        if (local != null)
        {
          local(this, args);
        }
      }
    }
    

    Update:

    Let me be a little more clear on why a memory barrier is necessary in this situation. The barrier prevents the read from being moved before other instructions. The most likely optimization is not from the CPU, but from the JIT compiler “lifting” the read of Progress outside of the while loop. This movement gives the impression of “stale” reads. Here is a semi-realistic demonstration of the problem.

    class Program
    {
        static event EventHandler Progress;
    
        static void Main(string[] args)
        {
            var thread = new Thread(
                () =>
                {
                    var local = GetEvent();
                    while (local == null)
                    {
                        local = GetEvent();
                    }
                });
            thread.Start();
            Thread.Sleep(1000);
            Progress += (s, a) => { Console.WriteLine("Progress"); };
            thread.Join();
            Console.WriteLine("Stopped");
            Console.ReadLine();
        }
    
        static EventHandler GetEvent()
        {
            //Thread.MemoryBarrier();
            var local = Progress;
            return local;
        }
    }
    

    It is imperative that a Release build is ran without the vshost process. Either one will disable the optimization that manifest the bug (I believe this is not reproducable in framework version 1.0 and 1.1 as well due to their more primitive optimizations). The bug is that “Stopped” is never displayed even though it clearly should be. Now, uncomment the call to Thread.MemoryBarrier and notice the change in behavior. Also keep in mind that even the most subtle changes to the structure of this code currently inhibit the compiler’s ability to make the optimization in question. One such change would be to actually invoke the delegate. In other words you cannot currently reproduce the stale read problem using the null check followed by an invocation pattern, but there is nothing in the CLI specification (that I am aware of anyway) that prohibits a future hypothetical JIT compiler from reapplying that “lifting” optimization.

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

Sidebar

Related Questions

I have a program that uses threads in C#. Is there a way to
I have a program that uses the mt19937 random number generator from boost::random. I
I have a program that uses JAMA and need to test is a matrix
I have a Python program that uses the threading module. Once every second, my
I have a Windows C# program that uses a C++ dll for data i/o.
I have a java client program that uses mdns with service discovery to find
I have a small command line program that uses the Team System API. When
If I have a program that uses threading and Queue, how do I get
I have a Java program that uses reflection to find and invoke the main(String[]
I have a python script that uses threads and makes lots of HTTP requests.

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.