简体   繁体   中英

How can I read a file in the UI thread

I have code like this but when i run it show ":

Synchronous operations should not be performed on the UI thread. Consider wrapping this method in Task.Run

How can i resolve this ? Thanks

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

Or use the async version of the API.

File.ReadAllLinesAsync

Also please read this post on how to use async in constructors, since you will need to mark a method as async in order to await on the API calls.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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