代码被取消了

时间:2013-12-24 00:15:56

标签: c# multithreading backgroundworker

我的背景工作者遇到了问题......它只是停止了......它没有通过它应该通过的所有代码......

在下面的代码中它只是停在String gName = comboBox1.SelectedItem.ToString();没有任何错误没有..下面的代码根本就没有运行..我通过在ZipFile pack = new ZipFile();放置一个断点来测试这个...断点不会被触发......我一遍又一遍地查看我的代码......我不明白为什么会这样做......

后台工作人员:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
   String appDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData).ToString();
   String gpsPath = appDataFolder + "/GameProfileSaver";
   String userDir = gpsPath + "/profiles/" + currentUserLabel.Text;

   XmlDocument doc = new XmlDocument();
   doc.Load(userDir + "\\games.xml");
   String gName = comboBox1.SelectedItem.ToString();
   ZipFile pack = new ZipFile();
   foreach (XmlNode node in doc.SelectNodes("//games/game[gameName='" + gName + "']/Files/file"))
   {
      try
      {
         if (!Directory.Exists(userDir + "\\" + gName))
         {
            Directory.CreateDirectory(userDir + "\\" + gName);
         }
         pack.AddFile(node.InnerText);
      }
      catch (Exception ex)
      {
         MessageBox.Show(ex.Message);
      }
   }
   pack.Save(userDir + "\\" + gName);
}

3 个答案:

答案 0 :(得分:4)

您应该只从UI线程访问UI元素。如果要使用组合框中的选定值,则应将其传递给后台工作程序。

您可以查看有关如何操作的答案:Sending Arguments To Background Worker?

答案 1 :(得分:3)

您无法从其他线程访问Windows窗体控件。

请参阅Cross-thread operation not valid: Control 'textBox1' accessed from a thread other than the thread it was created on了解解决方案。

答案 2 :(得分:0)

我的猜测是在该行之前有一个未被处理的异常。从Source开始,您尝试访问try catch之外的组合框。

You must be careful not to manipulate any user-interface objects in your DoWork event handler.
相关问题