Chapter 5. What are we (a)waiting for? An introduction to multithreading for Xamarin apps
This chapter covers
- What is a thread, and what is multithreading?
- What the UI thread is, and why it’s special
- Using tasks to run code on background threads
- Using async and await to make your code cleaner
When building apps, you always want to give your users the best experience possible. The world of mobile apps is highly competitive, with app users willing to drop your app for a competitor if you offer them a bad experience. There are many ways to provide this bad experience, but one of the worst is having an app with a slow, lagging UI, or one that locks up and becomes unresponsive. Fortunately, fixing apps that lag or lock up is relatively easy, and in this chapter we’ll look at ways to do this.
This chapter takes a dive into multithreaded code for mobile apps, covering the UI thread, Task, and async/await. If you’re an experienced C# UI developer (maybe from years of building desktop WPF apps), you’ve used tasks before, and if all your code uses async and await, you can feel free to skip this chapter. If your experience of multithreaded code is not UI-based, or not in C#, then read on! There are some quirks to be aware of when writing UIs that use multiple threads, and there are some awesome tools in the C# toolbox that can help.