首页 > 编程语言 > 详细

WPF多线程更改UI界面数据

时间:2021-03-28 11:12:34      阅读:31      评论:0      收藏:0      [点我收藏+]
public partial class MainWindow : Window
{

    private System.Windows.Threading.DispatcherTimer timer;
    public MainWindow()
    {
        InitializeComponent();
        timer = new System.Windows.Threading.DispatcherTimer();
        timer.Interval = TimeSpan.FromMilliseconds(1000);
        timer.Tick += timerTick;
        timer.Start();
    }

    private void timerTick(object sender, EventArgs e)
    {
        //WPF更改UI界面数据
        //解决方案:使用Dispatcher.BeginInvoke(委托方式{修改UI})

        //方案一:使用delegate
        /*this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, (ThreadStart)delegate ()
        {
            tbText.Text = Guid.NewGuid().ToString();
        });*/

        //方案二:Action
        this.Dispatcher.Invoke(new Action(() =>
        { tbText.Text = Guid.NewGuid().ToString(); }));
    }
}

  源码下载:WPFThread1.0

WPF多线程更改UI界面数据

原文:https://www.cnblogs.com/microsoft-zh/p/14587689.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!