繁体   English   中英

如何在UI线程中读取文件

[英]How can I read a file in the UI thread

我有这样的代码,但是当我运行它时显示“:

不应在UI线程上执行同步操作。 考虑将此方法包装在Task.Run中

我该如何解决? 谢谢

public guchi()
{   
    string[] list = File.ReadAllLines(@"C:\Users\sample.csv");

    foreach (string item in list)
    {
        var tmp = item.Split(';');
        this.Add(new guchi()
        {
           test = tmp[0],
            fun = tmp[1],
            happy = tmp[2],
            run = tmp[3],
            now = tmp[4],
            god = tmp[5],
            time = tmp[6],
            final= tmp[7],
            Pyke = tmp[8],
            Xinzhao = tmp[9]
        });
    }
}

或使用API​​的异步版本。

File.ReadAllLinesAsync

另外,请阅读此文章 ,了解如何在构造函数中使用异步,因为您需要将方法标记为异步才能等待API调用。

暂无
暂无

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

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