3

I'm having an issue with my background worker... It just stops.. It doesn't go through all the code that it is supposed to go through...

In the code below it just stops at String gName = comboBox1.SelectedItem.ToString(); With no errors nothing.. The code below simply does not get run.. I tested this by puting a breakpoint at ZipFile pack = new ZipFile();... The breakpoint does not get triggered... I have gone through my code over and over again... I can't find out why it does that...

Background worker:

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);
}
Brian
  • 5,069
  • 7
  • 37
  • 47
Stian Tofte
  • 213
  • 6
  • 13
  • I can't find any specific references right now, but IIRC `DoWork` can't access the form's elements. Try putting `String gName = comboBox1.SelectedItem.ToString();` inside the try/catch and see what happens – CDspace Dec 24 '13 at 00:22

3 Answers3

4

You should access UI elements only from the UI thread. If you want to use the selected value from the combo box, you should pass it to the background worker.

You can check this answer on how to do it: Sending Arguments To Background Worker?

Community
  • 1
  • 1
Szymon
  • 42,577
  • 16
  • 96
  • 114
3

You cannot access a windows form control from another thread.

See Cross-thread operation not valid: Control 'textBox1' accessed from a thread other than the thread it was created on for a solution.

Community
  • 1
  • 1
Richard Schneider
  • 34,944
  • 9
  • 57
  • 73
0

My guess is there is an exception before that line that isn't being handled. From the Source, and you are trying to access a comboBox outside of your try catch.

You must be careful not to manipulate any user-interface objects in your DoWork event handler.
CDspace
  • 2,639
  • 18
  • 30
  • 36