D
D
DancingOnWater2014-06-06 11:24:01
C++ / C#
DancingOnWater, 2014-06-06 11:24:01

How to pass data from external thread to GUI thread?

There is a code:

namespace WindowsFormsApplication1Test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var dataUpdate = new DataUpdate();
            dataUpdate.dE += setDataSource;
            new Thread(dataUpdate.doUpdate).Start();
        }

        public void setDataSource( Object obj)
        {
            listBox1.DataSource = obj;
        }

        class DataUpdate
        {
            public delegate void DataLIStEvent(Object obj);
            public event DataLIStEvent dE;
            public void doUpdate()
            {
                var value = new List<string>();
                value.Add("0.76");
                dE(value);
            }
        }
    }
}

It is necessary to transfer data from the doUpdate stream to setDataSource and so that the gui is updated

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Boxxy, 2014-06-06
@DancingOnWater

listBox1.Invoke(new Action(() => { listBox1.DataSource = obj }));
Will it do?
You can also create a property and push the same into the setter, but there will be value instead of obj.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question