2013-08-20 73 views
1

下面的代碼實際上做我想做的事情但是我試圖讓它最優化,並且即時通訊仍在學習BackgroundWorker等我想你可以給我一些提示。麻煩的是我可以運行該程序,但由於它的運行時間很長(每個文本文件有1000個+行),它只能在程序完成之前更新進度條或標籤。有2個progreesbars,一個用於處理文本文件的數量,另一個用於處理中文本文件的行數。BackgroundWorker C#,從txt文件讀取 - 寫入csv

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"); 
    } 
} 

}

+0

我沒有看到的BackgroundWorker參考這裏... – Medinoc

+0

@medinoc - 是的,我花了最後兩個小時試圖合併它,結果導致了該計劃的運作。然而,我會再次嘗試,並將其納入上述問題。 – Rg786

+0

問題是什麼? –

回答

2

對,一小時左右後,這就是我所做的。不是最優雅的,我仍然是新手,感謝nim(MSDN鏈接)。但是我有麻煩的進度顯示的進度正確量.....

namespace myParser 
{ 
public partial class Form1 : Form 
{ 
    string filePath; 
    string[] files; 
    int fileCount = 0; 
    int numberOfFiles; 
    int lineCount = 0; 
    int numberOfLines; 

    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; 
     btnOpen.Visible = false; 

     lbProcessedFiles.Visible = true; 
     pbFilesProcessed.Visible = true; 
     lbProcessedLines.Visible = true; 
     pbLinesProcessed.Visible = true; 
     lbProcessedLines.Text = "Lines: 0/0"; 

     pbFilesProcessed.Maximum = Convert.ToInt32(files.Length.ToString()); 

     backgroundWorker1.RunWorkerAsync(); 
    } 

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) 
    { 

     foreach (var csvFile in Directory.GetFiles(folderBrowserDialog1.SelectedPath)) 
     { 
      numberOfFiles = Convert.ToInt32(files.Length.ToString()); 

      string fileName = "Waveforms.csv"; 
      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]; 

      numberOfLines = File.ReadLines(csvFile).Count(); 

      using (StreamReader r = new StreamReader(csvFile)) 
      { 
       while ((lines = r.ReadLine()) != null) 
       { 
        if (lineCount == 0) 
        { 
         lines.Remove(0); 
         lineCount++; 
        } 
        else 
        { 
         newLines = Regex.Replace(lines, ",{2,}", ",").ToString(); 
         File.AppendAllText(@"Simulator\\" + fileName, pointName + "," + swingDirection + "," + newLines + System.Environment.NewLine); 
         backgroundWorker1.ReportProgress(lineCount); 
         lineCount++; 
        } 
       } 
       r.Close(); 
      } 
      backgroundWorker1.ReportProgress(fileCount); 
      fileCount++; 
     } 
    } 

    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) 
    { 
     pbFilesProcessed.PerformStep(); 
     lbProcessedFiles.Text = "Files: " + fileCount.ToString() + "/" + files.Length.ToString(); 
     pbLinesProcessed.PerformStep(); 
     lbProcessedLines.Text = "Lines: " + lineCount + "/" + Convert.ToInt32(numberOfLines.ToString()); 
    } 

    private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) 
    { 
     btnParse.Visible = false; 
     MessageBox.Show("Done"); 
     btnOpen.Visible = true; 
    } 
} 

}