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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:19:50+00:00 2026-06-06T14:19:50+00:00

I’m writing chat client/server app with c# and I have problem with threading. I

  • 0

I’m writing chat client/server app with c# and I have problem with threading. I wrote this simple code for showing my problem.

I used thread_1 for showing Form but it just show it a second (maybe thread_1 terminated and closed the Form , but i IsAlive said its alive !). Thread_2 try to reach texBox that created on main Thread but it shows me:

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

I dont know how solve first problem but I solved second problem with BackgroundWorker but i like to do it with thread. Is there any way?

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    Thread t1;
    Thread t2;


    private void button1_Click(object sender, EventArgs e)
    {

        t1 = new Thread(doThread1);
        t1.Name = "thread_1";

        t2 = new Thread(doThread2);
        t2.Name = "thread_2";

        t1.Start();
        t2.Start();

        MessageBox.Show(t1.IsAlive.ToString());
    }

    private void doThread1()
    {
        Form frm2 = new Form();
        frm2.Show();
    }


    private void doThread2()
    {
        try
        {
            for (int j = 10000; j > 0; j--)
                textBox.Text = j.ToString();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }


}
  • 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-06T14:19:52+00:00Added an answer on June 6, 2026 at 2:19 pm

    As Linkerro mentioned, you want to take out the thread you call Thread 1 as your UI will already be in a thread when you start (all programs have a single main thread they start on). You were on the right track though, you want to put any long-running tasks on a separate thread so it doesn’t block the UI. The only trick is you cannot directly maniuplate UI objects from background threads, they must be manipulated from the thread that owns them (which is the error message you are getting is saying).

    Luckily there is a very easy way to accomplish this in .NET. In WPF you use UiComponent.Dispatcher.Invoke() and Winforms just use UiComponent.Invoke(). This allows your background thread to step over to the thread where the UI component lives to update it.

    Invoke takes a delegate which represents the action you would like to run on the UI thread. In my example I pass in an action which is initialized using a lambada expression, taking no parameters and returning no value.

    Try this

    private void doThread2()
    {
        try
        {
            for (int j = 10000; j > 0; j--)
            {
                textBox.Invoke(new Action(() =>
                    textBox.Text = j.ToString()));
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
    

    Here is a full example of how you can do this with Tasks. You will see while it is counting you can freely move the window around and it does not lock up. However take out the Task and leave the loop and you will see how the window freezes since the loop would then block the UI thread.

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            Task.Factory.StartNew(() =>
            {
                for (int x = 0; x < 100000000; x++)
                {
                    label1.Invoke(new Action(() =>
                        label1.Text = x.ToString()));
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am writing an app with both english and french support. The app requests
I have some data like this: 1 2 3 4 5 9 2 6
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

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.