Cross-thread operation not valid: Control 'textBox1' accessed from a thread other than the thread it was created on.
what's wrong with this? i want to display the thread result to a text box. here's my code: thanks
what's wrong with this? i want to display the thread result to a text box. here's my code: thanks
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.Threading;
namespace MultiThread
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Do()
{
for (int i = 0; i <= 100; i++)
{
textBox1.Text = textBox1.Text + "hello";
Thread.Sleep(100);
}
}
private void button1_Click(object sender, EventArgs e)
{
//// Creates a new thread
ThreadStart tr = new ThreadStart(Do);
Thread t = new Thread(tr);
t.Start();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
}
public void TestThread()
{
for (int i = 0; i <= 1000; i++)
{
textBox1.Text = textBox1.Text + "Thread";
Thread.Sleep(100);
}
}
}
}
Comment