首页 > 代码库 > Winform 异步更新listbox

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