异步等待进度报告不起作用 [英] Async Await Progress Reporting Not Working

查看:54
本文介绍了异步等待进度报告不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C#WPF程序,该程序打开一个文件,一行一行地读取它,操纵每一行,然后将该行写到另一个文件中.那部分工作正常.我想添加一些进度报告,所以我使方法异步,并在等待进度报告时使用.进度报告非常简单-只需更新屏幕上的标签即可.这是我的代码:

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

任何帮助,建议或建议,将不胜感激.

Any help, advice or suggestions would be greatly appreciated.

推荐答案

仅将方法标记为async对执行流几乎没有影响,因为您永远不会产生执行.

Just marking your method as async has pretty much no effect on execution flow, because you're not yielding the execution ever.

使用ReadLineAsync代替ReadLine,并使用WriteLineAsync代替WriteLine:

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

这将产生UI线程并允许重画标签.

That will yield the UI thread and allow the label to be redrawn.

PS.编译器应该在您的初始代码中发出警告,因为您有一个不使用awaitasync方法.

PS. The compiler should have raised a warning with your initial code, because you have an async method which does not use await.

这篇关于异步等待进度报告不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆