Xamarin Forms: How to call bound service from content page? Is there any way to write code in separate thread?

173 views Asked by At

I am working on Xamarin Forms. In Content Page, I want to write some code in separate thread because app is freezing after some random interval.

In my code There is conversion of byte[] to string methods like below

BitConverter.ToString(Data); are taking time to execute, so anybody have resolution on it please share it.

Thanks in advance.

2

There are 2 answers

0
Nitish On

You can make your conversion method's return type to be Task and make it async. Then you can use .NET's multi-threading concept by using Task.Run. For example:

Converter method:

public async Task Converter()
{
// your conversion code
}

From the place you are calling this method:

await Task.Run(Converter);

While debugging, you can see in thread window that task will run in a separate thread. Enable thread window by ctrl+alt+H while debugging.

1
SushiHangover On

Use a Task-based thread from the thread pool via Task.Run:

await Task.Run( () =>
{
    // do some work....

    Device.BeginInvokeOnMainThread(() =>
    {
        // back on ui thread, hide progress indicator, show some dialog, etc... 
    });
});