/ Published in: C#
I didn't like the way ReportProgress events backed up, so I ran a Timer object to update the progress meter instead.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
int totalLines = 0; int goodLines = 0; int badLines = 0; int colNum = 0; long bytesProcessed = 0; long fileSize = 0; string regEx = string.Empty; char[] delimiter = null; StreamReader sr = null; private void txtParse_Click(object sender, EventArgs e) { totalLines = 0; goodLines = 0; badLines = 0; bytesProcessed = 0; progressBar1.Value = 0; fileSize = fi.Length; colNum = Convert.ToInt32(txtColNum.Text); regEx = txtRegex.Text; delimiter = txtDelimiter.Text.ToCharArray(); timer1.Start(); backgroundWorker1.RunWorkerAsync(); } private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { while(!sr.EndOfStream) { string rawLine = sr.ReadLine(); totalLines++; bytesProcessed += rawLine.Length; string[] splitLine = rawLine.Split(delimiter); if (Regex.IsMatch(splitLine[colNum], regEx)) { goodLines++; } else { badLines++; sb.AppendLine(rawLine); } } } private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { txtOutput.Text = "Lines Processed: " + totalLines.ToString() + " "; txtOutput.Text += "Good Lines: " + goodLines.ToString() + " "; txtOutput.Text += "Bad Lines: " + badLines.ToString() + " "; txtOutput.Text += sb.ToString(); timer1.Stop(); progressBar1.Value = 100; } private void timer1_Tick(object sender, EventArgs e) { double ratio = Convert.ToDouble(bytesProcessed) / Convert.ToDouble(fileSize); double rawPercent = ratio * 100; int send = Convert.ToInt32(rawPercent); progressBar1.Value = send; }