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

时间:2023-03-09 20:46:48
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);
}
}