crest dubua

crest dubua

  • NA
  • 19
  • 0

CROSS-THREAD in windows

Aug 30 2012 9:35 AM
Multithread :  CROSS-Thread  is not ALLOWED with WindowForms .
  *****  I require a specific answer not LINKs!!!************************-out
*******  Just comment CODE-Line I pointed-out & get it to WORK  all the same.


So I used the Trick to change UI-elements using 
  Invoke(new Action(() => TB1.Text="okkkkkk"));  //TB1 is a TextBox

But now see 
IF i insert         messageBox.Show("ciao");  // in MAIN-code [MainThread]  near end of code
everything works

if I comment out that line nothing works !
 
  is MessageBox a Magic-Formula or what ?????

using System.Threading;

namespace CROOS_THREAD_2012
{
public partial class F_37 : Form
{
public F_37()
{ InitializeComponent(); }//subNew

private void Form1_Load(object sender, EventArgs e)
{ }//FAb

//*******************************************************************
//*******************************************************************
// Remember That using ASYNCH_operations causes CODE to be EXCUTED on other-Thread than the MAIN
// BeginInvoke is EXECUTED on another-Thread [not on the MAIN-THREAD ]

public delegate int AsynchJO();


// We cannot change UI-elements in this code [TB1.Text="ppp" ...is not ALLOWED("CROSS_THREAD violation")]
public int JO()
{ MessageBox.Show("aa");
// Thread.Sleep(1000);
if (this.TB1.InvokeRequired)
{ MessageBox.Show("alllluw"); Invoke(new Action(() => TB1.Text="okkkkkk")); }
else
{ MessageBox.Show("agh"); TB1.Text = "99"; }

return 777;

}//JO_end



private void button1_Click(object sender, EventArgs e)
{
AsynchJO oo = new AsynchJO(JO);
IAsyncResult rR = oo.BeginInvoke(null, null);
Thread.Sleep(5000);
B1.Text = "Vai BABY";
MessageBox.Show("MAIN-page"); // If we comment-out this LINE everything will not WORK !!! [Mistero] --ZK() will never be reached !

Thread.Sleep(2000);
TB2.Text = oo.EndInvoke(rR).ToString();
B1.Text = "ERDAS";
}//click_END


}//CL
}//nam


Answers (1)