I'm trying to uncheck checkbox after timer stops. But somehow I receive an error on OnTimedEvent
method in this part checkBox1.Checked = false;
:
Cross-thread operation not valid: Control 'checkBox1' accessed from a thread other than the thread it was created on.
There is part of my code:
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
System.Timers.Timer timeris = new System.Timers.Timer();
int test = 0;
public Form1()
{
InitializeComponent();
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
comboBox1.Enabled = (checkBox1.CheckState == CheckState.Checked);
if (comboBox1.Enabled)
{
button1.Enabled = false;
}
else
{
button1.Enabled = true;
}
}
private void button1_Click(object sender, EventArgs e)
{
if (comboBox1.Enabled)
{
if (comboBox1.SelectedIndex < 0)
{
comboBox1.Text = "Select from list";
}
else
{
this.Hide();
timeris.Elapsed += new ElapsedEventHandler(OnTimedEvent);
timeris.Interval = Convert.ToInt32(test);
timeris.Start();
}
}
else
{
this.Close();
}
}
private void OnTimedEvent(object sender, EventArgs e)
{
timeris.Stop();
if (checkBox1.Checked == true)
{
checkBox1.Checked = false; // there I receive an error
}
this.Show();
}
}
Have you any ideas how could I solve It? If I build application and launch .exe
file It looks like working successfully, but I got an error while debugging.
Aucun commentaire:
Enregistrer un commentaire