【WPF学习】WPF、WinForm(C#)多线程编程并更新界面(UI)/子线程更新主界面方式

关于c#中多线程更新主界面的问题。c#

实例学习参考。。。。。多线程

常常忘记!!!!!学习

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using System.Threading;

namespace doWorker
{
    public partial class Form1 : Form
    {
        delegate void MyDelegate(int value);
        Thread t;
        int i = 0;
        public Form1()
        {
            InitializeComponent();
        }

        // 在新的线程中作“须要长时间作的”工做
        private void button1_Click(object sender, EventArgs e)
        {
            t = new Thread(doWork);
            t.Start();
        }

        // 要长时间作的工做
        void doWork()
        {
            MyDelegate d = new MyDelegate(setValue);
            while (true)
            {
                ++i;

                //---WinForm--
                this.Invoke(d, i);
                //----WPF---added by wonsoft.cn---
                this.Dispatcher.Invoke(d, i);

                Thread.Sleep(100);
            }
        }

        // 更新用户界面
        void setValue(int value)    
        {
            label1.Text = value.ToString();
        }

        // 终止线程的执行
        private void button2_Click(object sender, EventArgs e)
        {
            t.Abort();
        }
    }
}
原文连接:http://blog.csdn.net/wonsoft/article/details/7801177