我已經使用後臺工作人員在傳遞過程中多次使用DoWork去獲取數據,然後使用我相信在UI線程上運行的WorkerComplete來更新UI。但是,我的工作方式,我一直在得到一個交叉線程錯誤?在RunWorkerCompleted上更新UI
繼承人我有:
public partial class Form1 : Form
{
Public Form1()
{
}
BackgroundWorker CheckPopUps;
DataSet Popups = new DataSet();
public void Rotate()
{
CheckPopUps = new BackgroundWorker();
CheckPopUps.DoWork += CheckPopUps_DoWork;
CheckPopUps.RunWorkerCompleted += CheckPopUps_RunWorkerCompleted;
CheckPopUps.RunWorkerAsync();
}
void CheckPopUps_DoWork(object sender, DoWorkEventArgs e)
{
DataTable Pops1 = SharedTools.DataProcedures.Popup_GetListToPop(2, ScreenName, "Interviews");
if (Pops1.Rows.Count > 1) { Popups.Tables.Add(Pops1.Copy()); }
}
void CheckPopUps_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
foreach (DataTable myTable in Popups.Tables)
{
foreach (DataRow myRow in myTable.Rows)
{
PopUp Pop = new PopUp();
Pop.SetDetails(myRow);
this.Controls.Add(Pop);
//Error occurs on this line^
//Cross-thread operation not valid: Control 'Form1' accessed from a thread other than the thread it was created on.
}
}
}
}
'Pop'應該是lowecase'pop',因爲它是一個局部變量。 – ja72