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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:41:02+00:00 2026-05-25T14:41:02+00:00

This program is writing numbers from 1 to 5000 in thread, but main form

  • 0

This program is writing numbers from 1 to 5000 in thread, but main form freezes anyway.
Where is an error? Thanks in advance.

Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Threading;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        int how, current;
        bool job;
        Object lockobj = new Object();

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Started!");
            how = 5000;
            current = 0;
            job = true;
            Thread worker = new Thread(Go);
            worker.Name = "1";
            worker.Start();
        }

        private void Go()
        {
            while (job)
            {
                if (current < how)
                {

                    lock (lockobj)
                    {
                        current++;
                    }

                    log(string.Format("Thread #{0}: {1}", Thread.CurrentThread.Name, current));
                }
                else
                {
                    job = false;
                }
            }
        }


        private void log(string text)
        {
            Action A = new Action(() =>
            {
                richTextBox1.AppendText(text + System.Environment.NewLine);
            });

            if (richTextBox1.InvokeRequired)
                this.BeginInvoke(A);
            else A();
        }
    }


}
  • 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-25T14:41:02+00:00Added an answer on May 25, 2026 at 2:41 pm

    Because most of your work will be spent in

            if (richTextBox1.InvokeRequired)
                this.BeginInvoke(A);
    

    and while you invoke the form it is locked.

    Do some real work, like Thread.Sleep(1000); 🙂 , instead of current++; and your form will be response between the updates.

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

Sidebar

Related Questions

I am writing this java program to find all the prime numbers up to
Writing a python program, and I came up with this error while using the
I'm writing this small program to extract any number of email address from a
I'm writing this little program in shell: #!/bin/bash #*************************************************************** # Synopsis: # Read from
I've run into this while writing a Traveling Salesman program. For an inner loop,
I'm actually writing an MPI program. This is a basic client / server pattern.
I am writing a program that takes letters and converts them to telephone numbers.
I am writing a program to read lines of text from 5 files and
I am writing a large C program for embedded use. Every module in this
Hi I am writing a simple client-server program. In this program I have to

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.