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()
{
    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

Popular posts from this blog

c# Service Play Sound with NAudio example by Moshe

SOLVED The item could not be indexed successfully because the item failed in the indexing subsystem

JS/JQ simulate Enter event