一个小列子讲解线程、委托

在wpf中实现滚轮事件时,面中显示数据(从0开始,没滚动加1)

后台代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;

namespace WpfThreadDemo
{
    public delegate void Test();
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private Thread objThread = null;
        private int i = 0;
        public MainWindow()
        {
            InitializeComponent();
            this.MouseWheel += new MouseWheelEventHandler(MainWindow_MouseWheel);
        }

        void MainWindow_MouseWheel(object sender, MouseWheelEventArgs e)
        {
         
            objThread = new Thread(Change);
            objThread.IsBackground = true;
            objThread.Start();
            i++;
        }
        private void Change()
        {
            //Test test = new Test(Show);
            //this.Dispatcher.Invoke(test);
            this.Dispatcher.Invoke(new Action(delegate() { this.textBlock1.Text = i.ToString(); }), null);
        }
        private void Show()
        {
            this.textBlock1.Text = i.ToString();
        }

    }
}

 

posted @ 2013-03-28 23:13  焦涛  阅读(249)  评论(0)    收藏  举报