title: BackgroundWorker C#, Read from txt files - Write to a csv
body: <p>The following code actually does what i want it to do however I'm trying to make it optimal and as im still learning about the BackgroundWorker etc I thought you could give me a few tips. The trouble is I can run the program but because it's quite a long operation (1000's+ lines per text file), it doeant update the progressbar or labels very well only until the program completes. there are 2 progreesbars, one for the number of text files processed and the other for the number of lines read of the text file in the process.</p>
  
  <pre><code>namespace myParser
  {
      public partial class Form1 : Form
  {
  
      string filePath;
      string[] files;
  
      public Form1()
      {
          InitializeComponent();
  
          pbFilesProcessed.Visible = false;
          pbLinesProcessed.Visible = false;
          btnParse.Visible = false;
          lbProcessedFiles.Visible = false;
          lbProcessedLines.Visible = false;
      }
  
      private void btnOpen_Click(object sender, EventArgs e)
      {
          DialogResult result = folderBrowserDialog1.ShowDialog();
          if (result == DialogResult.OK)
          {
              //counts the number of files in the folder
              files = Directory.GetFiles(folderBrowserDialog1.SelectedPath);
  
              filePath = folderBrowserDialog1.SelectedPath.ToString();
  
              char[] delimiterChars = { '\\' };
              string[] filePathParts = filePath.Split(delimiterChars);
  
              string folder = filePathParts[4];
  
              tbFolderName.Text = folder;
  
              btnParse.Visible = true;
          }
      }
  
      private void btnParse_Click(object sender, EventArgs e)
      {
          btnParse.Visible = false;
  
          lbProcessedFiles.Visible = true;
          pbFilesProcessed.Visible = true;
          lbProcessedLines.Visible = true;
          pbLinesProcessed.Visible = true;
          lbProcessedLines.Text = "Lines: 0 / 0";
  
          int fileCount = 1;
  
          foreach (var csvFile in Directory.GetFiles(folderBrowserDialog1.SelectedPath))
          {
              pbFilesProcessed.Maximum = Convert.ToInt32(files.Length.ToString());
  
              string fileName = "Waveforms.csv";
  
              int lineCount = 0;
  
              string lines;
  
              string newLines;              
  
              string csvFullFilePath = csvFile.ToString();
              string[] filePathSplit = csvFullFilePath.Split('\\');
              string pointName = filePathSplit[4].ToString();
  
              string[] pathDirectionSplit = filePathSplit[5].ToString().Split('_');
              string[] swingDirectionSplit = pathDirectionSplit[3].Split('.');
              string swingDirection = swingDirectionSplit[0];
  
  
              var numberOfLines = File.ReadLines(csvFile).Count();
  
              using (StreamReader r = new StreamReader(csvFile))
              {
                  while ((lines = r.ReadLine()) != null)
                  {
                      pbLinesProcessed.Maximum = Convert.ToInt32(numberOfLines.ToString());
  
                      if (lineCount == 0)
                      {
                          lines.Remove(0);
                          lineCount++;
                      }
                      else
                      {
                          newLines = Regex.Replace(lines, ",{2,}", ",").ToString();
                          File.AppendAllText(@"Simulator\\" + fileName, pointName + "," + swingDirection + "," + newLines + System.Environment.NewLine);
  
                          pbLinesProcessed.PerformStep();
                          lbProcessedLines.Text = "Lines: " + lineCount + "/" + Convert.ToInt32(numberOfLines.ToString()); 
  
                          lineCount++;                                  
                      }
                  }
                  r.Close();
              }
              pbFilesProcessed.PerformStep();
              lbProcessedFiles.Text = "Files: " + fileCount.ToString() + "/" + files.Length.ToString();
              fileCount++;
          }
          btnParse.Visible = false;
          MessageBox.Show("Done");
      }  
  }
  </code></pre>
  
  <p>}</p>