简体   繁体   English

异步等待进度报告不起作用

[英]Async Await Progress Reporting Not Working

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. 我有一个C#WPF程序打开一个文件,逐行读取,操纵每一行然后将该行写入另一个文件。 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;
}

Any help, advice or suggestions would be greatly appreciated. 任何帮助,建议或建议将不胜感激。

Just marking your method as async has pretty much no effect on execution flow, because you're not yielding the execution ever. 只是将您的方法标记为async对执行流程几乎没有影响,因为您永远不会产生执行。

Use ReadLineAsync instead of ReadLine and WriteLineAsync instead of WriteLine : 使用ReadLineAsync而不是ReadLineWriteLineAsync而不是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. 这将产生UI线程并允许重绘标签。

PS. PS。 The compiler should have raised a warning with your initial code, because you have an async method which does not use await . 编译器应该使用您的初始代码发出警告,因为您有一个不使用awaitasync方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM