My knowledge was that accessing UI control will throw exception if it is accessed from the thread that is not the thread that created it (UI thread).
I got a code that does not throw exception when accessing simple properties of the UI control when run in Visual Studio with debugger.
var name = comboBox1.Name;
var m1 = comboBox1.Items[1].ToString();
It does when accessing SelectedIndex of the ComboBox.
comboBox1.SelectedItem.ToString();
The same code run without debugger does not throw exception? The same behavior is when double clicking on the exe, no exception?
Why there is only exception when code is run with debugger attached and not when running without debugger (Ctrl + F5) ?
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
comboBox1.Items.Add("one");
comboBox1.Items.Add("two");
comboBox1.Items.Add("three");
}
private void button2_Click(object sender, EventArgs e)
{
Task.Factory.StartNew(() =>
{
var name = comboBox1.Name;
var m1 = comboBox1.Items[1].ToString();
MessageBox.Show(comboBox1.SelectedItem.ToString(), "MM");
});
}
}