Avoid freezing.

Jassim Al Rahma 1,566 Reputation points
2022-02-22T19:49:10.107+00:00

Hi,

I have below code in my which allow the user Like ❤️ a Post just like Instagram likes for example

The problem is when I use Button Click or TapGesture with async like this:

async void LikeTapGestureRecognizer_Tapped(object sender, EventArgs e)

and if the user tried to like 3-4 post randomly in a short time then because if the async it will freeze for secodns everytime he Tap until the code is successfull.

How an I avoid this and let the code run at the background thread just like Instgarm, twitter and others..

Here is the code:

var client = new HttpClient();

client.BaseAddress = new Uri("https://www.domain.com/submit_like.php");

var content = new FormUrlEncodedContent(new[]
{
    new KeyValuePair<string, string>("post", "1"),
    new KeyValuePair<string, string>("user", SecureStorage.GetAsync("UserID").Result ),
});

var response = await client.PostAsync("https://www.domain.com/submit_like.php", content);

Thanks,
Jassim

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,336 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,842 questions
{count} votes

1 answer

Sort by: Most helpful
  1. P a u l 10,731 Reputation points
    2022-02-22T21:40:53.42+00:00

    Is there a reason you're using SecureStorage.GetAsync("UserID").Result instead of just await SecureStorage.GetAsync("UserID")?

    The former will block the UI thread & prevent messages from being pumped in the event loop.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.