Task.run and Async-Await's best practice: improve the UI response speed
When developing UI applications, it is important to keep the interface response smoothly. Choosing to use task.run or Await Async-Await will significantly affect the performance of the UI. This article will explore the advantages and disadvantages of these two architectures to help you make the best decisions.
Avoid blocking the UI thread
In order to prevent UI stuttering, please follow the following guidelines:
Keep the UI thread operation within 50 milliseconds
The number of continuing operation on the UI thread is limited to about 100 times per second -
- ingenious use of configureawait (false)
Configureawait (FALSE) can be used to indicate that you do not need AWAIT to restore execution in the current context (usually UI thread). Although this can improve performance, it must be noted that after using Configureawait (FALSE), avoid accessing UI elements or performing other context -related operations.
Task.run
When processing the CPU -intensive task, task.run can effectively uninstall the workload from the UI thread. However, it should be noted that task.run should only be used to call the CPU dense method, rather than integrating it into the implementation of the method itself.
CPU dense work method theory
CPU dense method should follow the following structure:
Document description: clearly pointed out that the method is CPU dense type.
How to use:
When calling this method, use task.run to uninstall the execution.
-
Mixed method
- The method of combining the CPU -intensive and I/O -intensive operation shall follow the following criteria:
asynchronous signature:
Use asynchronous signature design method.
Document description: CPU dense characteristics specified in the document.
Calling method:
Use task.run to start, because some CPU binding is applicable. The above is the detailed content of Task.Run vs. Async-Await: When Should I Use Each for Optimal UI Performance?. For more information, please follow other related articles on the PHP Chinese website!