locked
SynchronizationContext.Post to UI Method - update multiple labels RRS feed

  • Question

  • 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(string 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;
            }

    Wednesday, July 8, 2020 10:36 PM

Answers

  • Hi parvizb,

    Thank you for posting here.

    You can use List or Array as parameters, and then modify the value of the control.

            private void UIupdate(List<string> names)
            {
                WindowsFormsSynchronizationContext synchronizationcontext = new WindowsFormsSynchronizationContext();
                synchronizationcontext.Post(new SendOrPostCallback(o =>
                {
                    label1.Text = names[0];
                    label2.Text = names[1];
                }), names);
            }

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by parvizb Wednesday, July 22, 2020 8:09 PM
    Thursday, July 9, 2020 3:27 AM

All replies

  • Hi parvizb,

    Thank you for posting here.

    You can use List or Array as parameters, and then modify the value of the control.

            private void UIupdate(List<string> names)
            {
                WindowsFormsSynchronizationContext synchronizationcontext = new WindowsFormsSynchronizationContext();
                synchronizationcontext.Post(new SendOrPostCallback(o =>
                {
                    label1.Text = names[0];
                    label2.Text = names[1];
                }), names);
            }

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by parvizb Wednesday, July 22, 2020 8:09 PM
    Thursday, July 9, 2020 3:27 AM
  • Consider a generic approach like this:

    void UIupdate( Action action)

    {

       var timenow = DateTime.Now;

       if( (DateTime.Now - dt).TotalMilliseconds <= 50)

          return;

       synchronizationcontext.Post( o => action(), null );

       dt = timenow;

    }

    Usage:

    UIupdate( ( ) => { lblFirstName.Text = “first name”; lblLastName.Text = . . . } );

    Thursday, July 9, 2020 7:55 AM
  • Thank you very much.
    Friday, July 10, 2020 11:11 AM
  • Thank you very much.
    Friday, July 10, 2020 11:11 AM
  • Hi parvizb,

    Has your issue been resolved?

    If so, please click on the "Mark as answer" option of the reply that solved your question, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, July 13, 2020 1:20 AM