i am developing a code in which a have to read the data form list box and upload an external file, press a button named Start and with this listbox i am getting an error as shown below.
An unhandled exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll
Additional information: Cross-thread operation not valid: Control 'listBox1' accessed from a thread other than the thread it was created on.
my code is as follows
private void Start_Click(object sender, EventArgs e)
{
Thread ss = new Thread(Automode);
ss.Start();
}
private void Automode()
{
....
for (int i = 0; i < listBox1.Items.Count; i++)
{
listBox1.SetSelected(i, true);
string pattern = "[gxyzbcmij][-+]?[0-9]*\\.?[0-9]*";
string text = listBox1.Text;
Regex gcode = new Regex(pattern, RegexOptions.IgnoreCase);
MatchCollection code = gcode.Matches(text);
}
.....
}
here at listbox.SetSelected command it is giving an exception as shown above. please suggest the alternate ways to write it.