Created
April 17, 2018 18:18
-
-
Save hunterwei/3e17de366b5eac276c59cc7262d1a630 to your computer and use it in GitHub Desktop.
Calculate the data file processing percentage based of stream position.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void bgWorker_DoWork(object sender, DoWorkEventArgs e) | |
{ | |
// Details of obtaining file paths omitted for brevity | |
using (StreamWriter sw = new StreamWriter(outputFilePath, true)) //You can use this constructor instead of FileStream, it does the same operation. | |
using (StreamReader sr = new StreamReader(inputFilePath)) | |
{ | |
int lastPercentage = 0; | |
String line; | |
while ((line = sr.ReadLine()) != null) | |
{ | |
// Match and replace contents of the line | |
// omitted for brevity | |
//Poisition and length are longs not ints so we need to cast at the end. | |
int currentPercentage = (int)(sr.BaseStream.Position * 100L / sr.BaseStream.Length); | |
if (lastPercentage != currentPercentage ) | |
{ | |
bgWorker.ReportProgress(currentPercentage ); | |
lastPercentage = currentPercentage; | |
} | |
sw.WriteLine(line); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment