xdisgood
xdisgood

Reputation: 15

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

Upvotes: 0

Views: 174

Answers (1)

kennyzx
kennyzx

Reputation: 13003

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.

Upvotes: 2

Related Questions