Share via

SynchronizationContext.Post to UI Method - update multiple labels

pb 21 Reputation points
2020-07-08T23:04:59.207+00:00

In the following code snippet, How to update multiple labels? for example: Have several parameters in the update method instead of one parameter

 private void UIupdate(str
ing name)
        {
            var timenow = DateTime.Now;
            if((DateTime.Now-dt).Milliseconds<=50)
                return;
            synchronizationcontext.Post(new SendOrPostCallback(o =>
            {

                lblFirstName.Text = "name" + (string)o;
                //lblLastName.Text = ?
                //lblZipCode.Text=?
            }),name );
            dt = timenow;
        }
Developer technologies | Universal Windows Platform (UWP)

Answer accepted by question author

Daniele 1,996 Reputation points
2020-07-09T09:44:12.157+00:00

To update multiple labels you could do this.

private void UIupdate(string name, string lastName, string zipCode)
{
    synchronizationcontext.Post(new SendOrPostCallback(o =>
    {
        (string name, string last, string zipCode) state = ((string, string, string)) o;
        lblFirstName.Text = $"name {state.name}";
        lblLast.Text = $"last {state.last}";
        lblZipCode.Text = $"zip code {state.zipCode}";
    }), (name, lastName, zipCode));
}

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

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