Winform 异步更新listbox

    public partial class Form1 : Form
    {
        private BackgroundWorker bw;

        public Form1()
        {
            InitializeComponent();

            bw = new BackgroundWorker();
            bw.DoWork += (sender, args) => MethodToDoWork();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            bw.RunWorkerAsync();
        }

        private void MethodToDoWork()
        {
            for (int i = 0; i < 3; i++)
            {
                listBox1.BeginInvoke((Action)
                    (() => listBox1.Items.Add(string.Format("item {0}", i))));
                System.Threading.Thread.Sleep(1000);
            }
        }
    }

 

Winform 异步更新listbox

上一篇:使用el-select时选中值改变但没有渲染变化


下一篇:使用函数输出水仙花数