Ho una funzioneDiscussione Control.Invoke
public void ShowAllFly()
{
cbFly.Items.Clear();
cbFly.Items.Add("Uçuş Seçiniz...");
dsFlyTableAdapters.tblFlyTableAdapter _t=new KTHY.dsFlyTableAdapters.tblFlyTableAdapter();
dsFly _mds = new dsFly();
_mds.EnforceConstraints = false;
dsFly.tblFlyDataTable _m = _mds.tblFly;
_t.Fill(_m);
foreach (DataRow _row in _m.Rows)
{
cbFly.Items.Add(_row["FlyID"].ToString()+"-"+_row["FlyName"].ToString() + "-" + _row["FlyDirection"].ToString() + "-" + _row["FlyDateTime"].ToString());
}
_Thread.Abort();
timer1.Enabled = false;
WaitPanel.Visible = false;
}
In Form_Load funzione come questa;
{
_Thread = new System.Threading.Thread(new System.Threading.ThreadStart(ShowAllFly));
_Thread.Start();
_Thread.Priority = System.Threading.ThreadPriority.Normal;
}
Ma quando lo eseguo;
in funzione ShowAllFly
cbFly.Items.Clear(); ---- HERE Gives ERROR LIKE Control.Invoke must be used to interact with controls created on a separate thread.
Qual è il problema?
buona soluzione; di solito raccomando di usare l'azione dal framework piuttosto che rotolare (e mantenere) il tuo. –
Sì ... questo è praticamente il modo standard per farlo. Il motivo per cui questo è necessario è che textBox1.Text può essere modificato solo sul thread in cui è stata creata la textbox - il richiamo è il processo utilizzato per tornare a quel thread. –
@Fredrik: hai ragione, ma questo esempio è visto più spesso, in gran parte perché è durato più a lungo. Entrambi i metodi funzioneranno. –