Performance Tips - ChrispyPeaches/FocusFriends GitHub Wiki

Purpose

When you are able, offloading tasks from the main (UI) thread can help with the user experience. However, while doing this it's important to make sure that whenever code updates the UI, it's done on the main (UI) thread.

Tips

What should I run on the main thread?

  • Anything that directly updates the UI such as changing the property of a UI element or updating its content

What should I run on non-main threads?

  • Snippets of code that depend on things other than quick logic such as database calls, API calls, or long-running calculations.
  • Non-direct UI updates such as updating a property that a UI element is bound/subscribed to

How do I make something run on the main (UI) thread?

The linked article below details how to run code on the UI thread. Details can be found here

How do I offload something onto a non-main thread?

More details can be found here

  • Using Task.Run and other methods like it offload the passed method onto another thread
  • Be sure that whenever UI elements are updated inside of these passed methods, that it uses the MainThread.BeingInvokeOnMainThread method or methods like it discussed in the How do I make something run on the main (UI) thread section