主副线程的交互我记录下 也许不对。同事教我的。我自己去的名字。理解问题。

public partial class Page : UserControl
    {
        delegate void SetTextCallback(string text);

        public Page()
        {
            InitializeComponent();
            TextBlockTest.Text = "5秒钟后变化";
            Thread t = new Thread(new ThreadStart(Run));
            t.Start();
        }

        int timer = 5;

        private void Run()
        {
            SetTextCallback call = new SetTextCallback(SetText);
            while (timer > 0)
            {
                timer--;
                //call(timer + "秒钟后变化");
                this.Dispatcher.BeginInvoke(call, timer + "秒钟后变化");
                Thread.Sleep(1000);
            }
            this.Dispatcher.BeginInvoke(call,"变化结束!");

        }

        private void SetText(string text)
        {
            TextBlockTest.Text = text;
        }
    }

写下从学校走出来后,在北京实习的历程。

努力 加油!

原文地址:https://www.cnblogs.com/tonygao/p/1353698.html