UI unresponsive until action is complete

前端 未结 2 766
一个人的身影
一个人的身影 2021-01-03 16:25

I\'m not sure if the Title is a good description of this issue or not. Essentially what I have is a WinForm app that retrieves a list of files from a folder into a ListView,

2条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-03 17:06

    You could offload the long-running operation to a ThreadPool thread, by using async/await and the handy Task.Run method:

    transferResult = await Task.Run(() => session.PutFiles(readyFile, destFile, false, transferOptions));
    

    ...and:

    transferResult = await Task.Run(() => session.PutFiles(triggerFile, destFile, false, transferOptions));
    

    You should also add the async modifier in the event handler, in order to enable the await operator.

    Important: Avoid doing anything UI related in the offloaded method. If you want to communicate with the UI during the operation, for example for progress reporting, use the Progress class.

提交回复
热议问题