WinForms multi-threaded databinding scenario, best practice?

后端 未结 8 1470
渐次进展
渐次进展 2020-12-25 09:23

I\'m currently designing/reworking the databinding part of an application that makes heavy use of winforms databinding and updates coming from a background thread (once a se

8条回答
  •  暖寄归人
    2020-12-25 10:06

    Create a new UserControl, add your control and format it (maybe dock = fill) and add a property. now configure the property to invoke the usercontrol and update your element, each time you change the property form any thread you want!

    thats my solution:

        private long value;
        public long Value
        {
            get { return this.value; }
            set
            {
                this.value = value;
    
                UpdateTextBox();
            }
        }
    
        private delegate void Delegate();
        private void UpdateTextBox()
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Delegate(UpdateTextBox), new object[] {});
            }
            else
            {
                textBox1.Text = this.value.ToString();
            }
        }
    

    on my form i bind my view

    viewTx.DataBindings.Add(new Binding("Value", ptx.CounterTX, "ReturnValue"));
    

提交回复
热议问题