wPF,解决UI界面实时更新的问题

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Thread thread = new Thread(new ThreadStart(Run));
            thread.IsBackground = true;
            thread.Start();
        }


        public void Run()
        {
            for (int i = 0; i < 1000; i++)
            {
                this.Dispatcher.BeginInvoke((Action)delegate() 
                {
                    string passedSns = i + Environment.NewLine + this.textBox1.Text;
                    this.textBox1.Text = passedSns;
                    textBox1.Foreground = new SolidColorBrush(Colors.Red);
                });
               
                //给界面更新TextBox的时间
                Thread.Sleep(10);
            }
        }

 

posted @ 2014-05-20 09:25  Simon Lau  阅读(3680)  评论(0编辑  收藏  举报