我需要并行执行 2 个任务。将在 GUI 中加载数据,直到那时我想在用户面前连续运行一个进度条。我尝试了 BackgroundWorker,但它给了我一些线程同步错误。有人可以建议我做同样的任何其他最佳方式。
代码: backgroundWorker1 初始化:
backgroundWorker1 = new BackgroundWorker();
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
if (backgroundWorker1.IsBusy != true)
{
backgroundWorker1.RunWorkerAsync();
}
以下行出现错误:
XmlDocumentHierarchy _remoteObj = new XmlDocumentHierarchy(comboBox2.Text, "username", "password");
是:
"Cross-thread operation not valid: Control 'comboBox2' accessed from a thread other than the thread it was created on."