WinForms Bypass Cross Thread Update Error example
we can do 2 things
the 1st, not everybody loves it (and i guess that in large slns u should avoid it) is the
CheckForIllegalCrossThreadCalls prop:.
public Form1()
Form1.CheckForIllegalCrossThreadCalls = false;
}
private void button1_Click(object sender, EventArgs e)
{
Thread t = new Thread(new ThreadStart(target));
t.Start();
}
private void target()
{
this.Text = "ddddd";
}
the other way is to use a delegate:
delegate void SetTextCallback(string text);
private void SetText(string text)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (this.textBox1.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else
{
this.textBox1.Text = textBox1.Text + text;
}
}
p.s. - thanks to rabi dot net and stack overflow
the 1st, not everybody loves it (and i guess that in large slns u should avoid it) is the
CheckForIllegalCrossThreadCalls prop:.
public Form1()
{
InitializeComponent();Form1.CheckForIllegalCrossThreadCalls = false;
}
private void button1_Click(object sender, EventArgs e)
{
Thread t = new Thread(new ThreadStart(target));
t.Start();
}
private void target()
{
this.Text = "ddddd";
}
the other way is to use a delegate:
delegate void SetTextCallback(string text);
private void SetText(string text)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (this.textBox1.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else
{
this.textBox1.Text = textBox1.Text + text;
}
}
p.s. - thanks to rabi dot net and stack overflow
Comments
Post a Comment