At first Excuse me for asking this question.
I have just entered in topic of Threads and I’m beginner in this topic.
I have a problem with my program in runtime like this:
“Cross-thread operation not valid: Control ‘listBox1’ accessed from a thread other than the thread it was created on.”
I think my program is very very simple. But it has errors.
Please help me!
Thanks!
I expect the program that when I click the button1, it will adds some numbers with sum strings in the listBox1.
I declare a for loop that iterates from 0 to 9.
The error message is above and my very simple codes are below:
type here
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;
namespace Threads_01
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
System.Threading.Thread Thread_01;
private void button1_Click(object sender, EventArgs e)
{
Thread_01.Start();
}
private void function_01()
{
string item;
for (int i = 0; i < 10; i++)
{
item = "Hello abc (" + i.ToString() + ")";
listBox1.Items.Add(item);
}
}
private void Form1_Load(object sender, EventArgs e)
{
//Thread_01 = new System.Threading.Thread(function_01, 10);
Thread_01 = new System.Threading.Thread(function_01);
}
}
}
//Those are the codes of file: “Form1.cs”.
//and the designer codes are:
namespace Threads_01
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.listBox1 = new System.Windows.Forms.ListBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(44, 36);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(102, 48);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// listBox1
//
this.listBox1.FormattingEnabled = true;
this.listBox1.Location = new System.Drawing.Point(44, 116);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(225, 277);
this.listBox1.TabIndex = 1;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(683, 447);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.ListBox listBox1;
}
}
//and the program.cs codes are:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace Threads_01
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
AliRezaa RJ45 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.