await and async blocking the UI

前端 未结 2 1537
北海茫月
北海茫月 2021-01-13 02:26

I wrote a little winforms application that search for files on the disk (what file is not that important for the sake of the question). the problem is the that it can be eve

2条回答
  •  野的像风
    2021-01-13 02:48

    The fact of marking SearchPnrFilesAsync with async keyword itself doesn't magically starts execution ot this method asynchronously in separate task.

    In fact, all of the code in SearchPnrFilesAsync except sw.WriteAsync executes in UI thread thus blocking it.

    If you need to execute your whole method in separate task, you can do it by wrapping like:

    public async static Task SearchPnrFilesAsync(string mainDir)
    {
       await Task.Run(() => your_code_here);
    }
    

提交回复
热议问题