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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T03:11:53+00:00 2026-05-20T03:11:53+00:00

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text;

  • 0
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.Threading;


namespace testThreads
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        public void countToLots()
        {
            for (int i = 0; i < 10000000; i++)
            {
                textBox1.Text = "Counting to 10000000, value is " + i + Environment.NewLine;
            }
        }

        public void countToZero()
        {
            for (int i = 10000000; i > 0; i--)
            {
                textBox2.Text = "Counting to 0, value is " + i + Environment.NewLine;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Thread countUp = new Thread(new ThreadStart(countToLots));
            Thread countDown = new Thread(new ThreadStart(countToZero));
            countUp.Start();
            countDown.Start();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox3.Text = "Bobby bob bob " + Environment.NewLine;
        }
    }
}

I really need to try and get the hang of this – i just dont understand the theory behind why i get an error message. Could someone help me out please?

Cross-thread operation not valid:
Control ‘textBox1’ accessed from a
thread other than the thread it was
created on.

  • 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-20T03:11:54+00:00Added an answer on May 20, 2026 at 3:11 am

    UI controls have “thread affinity”; they do not want to be touched by anything except the UI thread; that includes reading and writing properties. The assignment to .Text should be done from the UI thread, either by using Invoke, or BackgroundWorker.

    For example:

    public void countToLots()
    {
        for (int i = 0; i < 10000000; i++)
        {
            // running on bg thread
            textBox1.Invoke((MethodInvoker) delegate {
                // running on UI thread
                textBox1.Text = "Counting to 10000000, value is "
                          + i + Environment.NewLine;
            });
            // running on bg thread again
        }
    }
    

    But note that this type of thread switching has overhead. You should not call back every iteration – you should (for example) update the UI every [n] iterations – in the above, every 10000 for example.

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

Sidebar

Related Questions

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text;
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text;
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text;
using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; namespace ConsoleApplication1 { public
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace GenericCount { class Program {
here's the code: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using
Consider the following snippet: using System; using System.Collections.Generic; using System.Linq; using System.Net; namespace ForumLogins
i have the following code: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using
i have the following code: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using
I am working with two timers: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data;

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.