WPF MultiThread

时间:2013-01-31 20:34:58

标签: c# wpf asynchronous task-parallel-library task

我需要加载一个大文件并在datagrid中显示数据,但有些我不能异步加载文件。

按钮代码:

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

}

这一个工作,但它根本不光滑,我无法移动窗口

1 个答案:

答案 0 :(得分:0)

请尝试使用此代码:

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