Xamarin - Working with threads

Mobile devices are optimised to run complex tasks on multiple threads. Running any form of complex task on the UI thread (the main thread) will cause the UI of your application to hang (or at least seem to anyway) until the operation is complete. The fix is to run the task on another thread and return a result back to the UI thread.

So if you find your aplication seemingly becoming unresponsive whilst running a complex or long running task like fetching a user profile from the web, then remember, run it in another thread and have a think about what other opperations can be pushed to another thread. The performance improvement is worthwhile too, since most new phones hitting the market have at least four CPU cores (some even come with eight!) the phone is more than capable of running multiple tasks at once. Your users will be thanking you too.

So running something on another thread is simple enough, simply make a new task...

Task.Run(async () =>
{
    // Run code here
})

Now if you add your code to a new task, it'll run on another thread. Just be aware that if you want to effect anything in the UI from this thread, the system will prevent this and throw an exception. To get around this, you'll need to add another small piece of code that will execute the UI action on the UI thread, here's how:

Device.BeginInvokeOnMainThread(() =>
{
    // UI interaction goes here
});

That's pretty much it. Almost anything can be run inside another thread. Here's an example of everything together so c=you can see how it's constructed:

Task.Run(async () =>
{
    // Run code here

    Device.BeginInvokeOnMainThread(() =>
    {
        // UI interaction goes here
    });
})

Good luck!


Published at

Tags: Xamarin,C#,Multi Tasking

Luke Alderton

Comments

praveen
Nice article, I have a doubt in task.run is it run asynchronously
05/10/2018
Shahriar H. Razi
Thanks, really great article.
07/05/2019
Paul
Thank you for this. Just learning, and this was clearly explained.
02/11/2019
George
Crisp and clear article.
10/03/2020
Alexander Gontar
Great! It works! Thanks a lot!
13/03/2020
Andy
works! Thanks..
12/05/2020
Aquif
Thanks, Nice tutorial.
09/07/2020
Post a comment
Sit tight...
We're adding your comment.
Thank you.
Your comment has been added.
There's been a problem.
Please try again later.
Share with
Tags
Latest Comments
By Ivan on Fixed: Value cannot be null Parameter Name: source
15 Sep 2020, 19:47 PM
By Anand on Raspberry Pi - Bluetooth using Bluecove on Raspbian
7 Sep 2020, 16:53 PM
By Aquif on Xamarin Forms - Working with threads
9 Jul 2020, 13:50 PM
By Chris on Disabling XAML Hot Reload
10 Jun 2020, 15:23 PM
By Ben on Raspberry Pi - Running Java app on Raspbian
24 May 2020, 09:01 AM
By BP on Disabling XAML Hot Reload
14 May 2020, 21:59 PM
By Andy on Xamarin Forms - Working with threads
12 May 2020, 09:16 AM
By mitchell on Xamarin forms - Stop/Cancel back button event
29 Apr 2020, 18:16 PM
Categories
App Development
Event
Game Development
Mapping
Modelling
Programming
Review
Robotics
Tutorial
Web Development