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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:59:24+00:00 2026-05-12T16:59:24+00:00

As a training exercise, I am creating a counter that runs in a background

  • 0

As a training exercise, I am creating a counter that runs in a background thread, in a .NET 3.5 WPF app. The idea is that the counter gets incremented and every few seconds the total is output to the screen.

As it is a training exercise, the foreground thread doesn’t really do anything, but imagine that it does!

There is a start and stop button to start and stop the counter.

The problem is that I want the foreground thread to poll the total every few seconds and update it in the text box on the form. The entire app goes ‘Not Responding’ when I try this. Currently I have only got it to update the count a fixed time after it starts it.

I’ll dump all the code in here, so that you get the whole picture:

 public partial class Window1 : Window
    {
        public delegate void FinishIncrementing(int currentCount);
        private int reportedCount;
        private Thread workThread;

        public Window1()
        {
            InitializeComponent();
        }

        #region events

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            txtCurrentCount.Text = "0";
            reportedCount = 0;
            InitialiseThread();
        }

        private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            workThread.Start();

            // Do some foreground thread stuff

            Thread.Sleep(200); // I want to put this in a loop to report periodically
            txtCurrentCount.Text = reportedCount.ToString();
        }

        private void btnStop_Click(object sender, RoutedEventArgs e)
        {
            workThread.Abort();
            txtCurrentCount.Text = reportedCount.ToString();
            InitialiseThread();
        }

        #endregion

        private void InitialiseThread()
        {
            WorkObject obj = new WorkObject(ReportIncrement, reportedCount);
            workThread = new Thread(obj.StartProcessing);
        }

        private void ReportIncrement(int currentCount)
        {
            reportedCount = currentCount;
        } 
    }

    public class WorkObject
    {
        private Window1.FinishIncrementing _callback;
        private int _currentCount;

        public WorkObject(Window1.FinishIncrementing callback, int count)
        {
            _callback = callback;
            _currentCount = count;
        }

        public void StartProcessing(object ThreadState)
        {
            for (int i = 0; i < 100000; i++)
            {
                _currentCount++;

                if (_callback != null)
                {
                    _callback(_currentCount);
                    Thread.Sleep(100);
                }
            }
        } 
    }

My question is: how do I get it to report periodically, and why does it fail when I try to put the reporting in a for loop?

Edit: Should have mentioned that my training exercise was to familiarise myself with the old-school method of multithreading, so I cannot use BackgroundWorker!

  • 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-12T16:59:24+00:00Added an answer on May 12, 2026 at 4:59 pm

    Every form in a windows application has a message loop that looks something like this:

    while (NotTimeToCloseForm)
    {
        Message msg = GetMessageFromWindows();
        WndProc(msg);
    }
    

    This applies to all windows applications not just .Net ones. WPF provides a default implementation of WinProc that goes something like this:

    void WndProc(Message msg)
    {
        switch (msg.Type)
        {
            case WM_LBUTTONDOWN:
                RaiseButtonClickEvent(new ButtonClickEventArgs(msg));
                break;
    
            case WM_PAINT:
                UpdateTheFormsDisplay(new PaintEventArgs(msg));
                break;
    
            case WM_xxx
                //...
                break;
    
            default:
                MakeWindowsDealWithMessage(msg);
        }
    }
    

    As you can see this means that a window can only process one message/event at a time. As you are sleeping in your Button Click event the message loop is being held up and the application stops responding (getting and processing messages).

    If you use a DispatcherTimer it asks windows to periodically stick a WM_TIMER (Tick) message in the windows message queue. This allows your application to process other messages while still being able to do something at regular intervals without using another thread.

    For more detail see MSDN “About Messages and Message Queues“

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

Sidebar

Related Questions

I am building a training system in Asp.net coded in C# that will allow
so i have this app that is a training program in this app the
I'm new to WPF/XAML & I'm just doing a training exercise at the moment.
I am undergoing training in asp.net and the topics which are being taught there
When creating a libsvm training file, how do you differentiate between a nominal attribute
I'm creating a training lab for a desktop application. Basically it'll be a series
I'm designing a training program in C++ that will be distributed to a large
I have a training set that has input and outputs in this way: Input:
I have been writing some C# code for a training exercise, in which I
As a self-development exercise, I want to develop a simple classification algorithm that, given

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.