Updating UI label

Hello,

I’m working on a Galaxy Watch widget. There is 1 label and 1 button on ui. When I press the button, I’m checking a service and get the data from it, while these operations continues I’m not able to update UI because I couldn’t find any thread/invoke information about Tizen .Net.

so far I’ve tried thread,background worker but still I’m not able to use ui thread to update my label.

Could you help me?

Thank you,
Mustafa

What do you mean by “a service” and what does it do? Can’t the work be done by using normal Asynchronous programming (Tasks with async/await)? You don’t typically need to create threads to do async operations like HTTP requests.

Also there’s nothing special about Tizen .NET. It’s based on a plain .NET Core runtime and you can use any C# language features and standard .NET libraries within your app code.

You may also elaborate the exact UI toolkit name (e.g. Xamarin.Forms) used by your app so that we can provide a better answer.

I’m using ElmSharp. So basically I’m searching for threading documentation. I’m getting content of an url, json data and I’m parsing it and display on widget. While getting content of url I want to keep updating main ui to inform user like please wait, processing, like keep showing elapsed time in seconds etc…

Do you have any suggestion how can I achieve it?

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).

Hi swift-kim,

Thank you. I’ll try. on the other hand, elmsharp doesn’t have some capabilities so for those who interest this question Xamarin.Forms Tizen have much more have capabilities.