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));
 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 });
       this.textBox1.Text = textBox1.Text + text;
p.s. - thanks to rabi dot net and stack overflow


Popular posts from this blog

c# Service Play Sound with NAudio example by Moshe

Asp.Net Ending Response options, Response.End() vs CompleteRequest()

JS/JQ simulate Enter event