Update Windows Form UI thread from another class c#

delegate void UpdateLabelDelegate(string LabelText);
public partial class Form1 : Form
{
        private Class1 _class;
        UpdateLabelDelegate ULD;
        Dispatcher uiDispatcher;    

        public void UpdateLabel(string LabelText)
        {
            lbl_ClientCount.Text = LabelText;          
        }

        public void UpdateThread(string LabelText)
        {
            uiDispatcher.Invoke(ULD, new object[] { LabelText });
        }
               
        public Form1()
        {

            InitializeComponent();
            _class = new Class1(this);
            ULD = new UpdateLabelDelegate(UpdateLabel);
            uiDispatcher = Dispatcher.CurrentDispatcher;          
            _class.UpdateUI()
        }
}

class Class1
{
        public static int ClientConnected = 0;

        private Form1 _form;
        public Class1(Form1 form)
        {
            //
            this._form = form;
        }

       public void UpdateUI()
      {
               ClientConnected++;
                _form.UpdateThread(Convert.ToString(ClientConnected));
      }      
}
    

Comments

Popular posts from this blog

SQL Interview Questions and Answers

Generic Interface and Factory Pattern in C#

How to get device info IMEI programmatically in xamarin android