Getting content of an URL is an I/O-bound operation and parsing JSON data is a CPU-bound operation. So you might write code like this:
// Change the text of a label to "Please wait...".
// I don't personally have much experience with ElmSharp so I'll skip details.
...
// Retrieve content from web.
var response = await httpClient.GetAsync("https://...");
var jsonString = await response.Content.ReadAsStringAsync();
// Update the label to display "Processing...".
...
// Process the JSON data on a thread pool thread.
// If the operation takes negligibly short time, you may simply remove
// `await Task.Run` and do the operation on the UI thread.
var book = await Task.Run(() =>
{
return JsonSerializer.Deserialize<Book>(jsonString);
});
// Update the widget.
...
If you also want elapsed time to be shown and updated periodically, you may poll with
a CancellationToken.
public async Task ContinuouslyUpdateTextAsync(CancellationToken token)
{
// update text...
await Task.Delay(1000, token);
if (!token.IsCancellationRequested)
{
await ContinuouslyUpdateTextAsync(token);
}
}
You can call the above method (without awaiting) before downloading JSON data and cancel the associated CancellationTokenSource after processing of the data.
If you’re using Xamarin.Forms with the MVVM pattern, there are other options like data binding but probably that’s not what you want at the moment. Microsoft’s documentation will help you a lot if you want to learn more about asynchronous programming in C# (although some of their patterns are pretty stale or may not fit your specific use case).