1

我需要加载一个大文件并在数据网格中显示数据,但有些我无法异步加载文件。

按钮代码:

private async void MILoadLogFile_Click(object sender, RoutedEventArgs e) {
    ...
        await myLogSession.LoadfromFileAsync(oFD.FileName);
    }
}

和加载方法

public async Task LoadfromFileAsync(String fileName) {

    StreamReader streamReader = new StreamReader(fileName);

    String line;
    try {
        using (streamReader) {
            while ((line = await streamReader.ReadLineAsync()) != null) {

                ...
            }
            ...
        }
        Console.Out.WriteLine("DOOOOOOOOOOOOONE");
    }
    } catch (Exception e) {
        Console.WriteLine("The file could not be read:");
        Console.WriteLine(e.Message);
    }
}

}

这一个工作,但它根本不平滑,我不能移动窗口

4

1 回答 1

0

尝试改用这个:

private async void MILoadLogFile_Click(object sender, RoutedEventArgs e)
{
    //...
    await Task.Factory.StartNew(async () =>
    {
        await myLogSession.LoadfromFileAsync(oFD.FileName);
    });
}
于 2013-01-31T21:46:02.923 回答