WPF MultiThread

本文关键字:MultiThread WPF | 更新日期: 2023-09-27 17:56:48

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

按钮代码:

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

}

这个有效,但它根本不平滑,我无法移动窗口

WPF MultiThread

尝试改用这个:

private async void MILoadLogFile_Click(object sender, RoutedEventArgs e)
{
    //...
    await Task.Factory.StartNew(async () =>
    {
        await myLogSession.LoadfromFileAsync(oFD.FileName);
    });
}