Reputation: 1368
I have a C# WPF program that opens a file, reads it line by line, manipulates each line then writes the line out to another file. That part worked fine. I wanted to add some progress reporting so I made the methods async
and used await
with progress reporting. The progress reporting is super simple - just update a label on the screen. Here is my code:
async void Button_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Title = "Select File to Process";
openFileDialog.ShowDialog();
lblWaiting.Content = "Please wait!";
var progress = new Progress<int>(value =>
{
lblWaiting.Content = "Waiting "+ value.ToString();
});
string newFN = await FileProcessor(openFileDialog.FileName, progress);
MessageBox.Show("New File Name " + newFN);
}
static async private Task<string> FileProcessor(string fn, IProgress<int> progress)
{
FileInfo fi = new FileInfo(fn);
string newFN = "C:\temp\text.txt";
int i = 0;
using (StreamWriter sw = new StreamWriter(newFN))
using (StreamReader sr = new StreamReader(fn))
{
string line;
while ((line = sr.ReadLine()) != null)
{
// manipulate the line
i++;
sw.WriteLine(line);
// every 500 lines, report progress
if (i % 500 == 0)
{
progress.Report(i);
}
}
}
return newFN;
}
But the progress reporting is not working.
Any help, advice or suggestions would be greatly appreciated.
Upvotes: 4
Views: 1802
Reputation: 43429
Using asynchronous I/O APIs, as suggested by MarcinJuraszek, solves the problem, but the built-in .NET asynchronous I/O APIs are not as performant as the corresponding synchronous APIs, so you may prefer not to use them. An alternative is to offload the invocation of the FileProcessor
to the ThreadPool
, using the Task.Run
method:
string newFN = await Task.Run(() => FileProcessor(openFileDialog.FileName, progress));
This way you can ensure that the performance will be preserved, that the progress reporting will work, and that the UI thread will not be blocked (unless you report progress too frequently, which I can see that you are careful enough not to do).
Upvotes: 1
Reputation: 125620
Just marking your method as async
has pretty much no effect on execution flow, because you're not yielding the execution ever.
Use ReadLineAsync
instead of ReadLine
and WriteLineAsync
instead of WriteLine
:
static async private Task<string> FileProcessor(string fn, IProgress<int> progress)
{
FileInfo fi = new FileInfo(fn);
string newFN = "C:\temp\text.txt";
int i = 0;
using (StreamWriter sw = new StreamWriter(newFN))
using (StreamReader sr = new StreamReader(fn))
{
string line;
while ((line = await sr.ReadLineAsync()) != null)
{
// manipulate the line
i++;
await sw.WriteLineAsync(line);
// every 500 lines, report progress
if (i % 500 == 0)
{
progress.Report(i);
}
}
}
return newFN;
}
That will yield the UI thread and allow the label to be redrawn.
PS. The compiler should have raised a warning with your initial code, because you have an async
method which does not use await
.
Upvotes: 6