基本信息
源码名称:多线程操作ui(入门级)
源码大小:0.05M
文件格式:.rar
开发语言:C#
更新时间:2020-02-20
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍


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 WpfThread
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Thread backThread = new Thread(new ThreadStart(TestThread));
            backThread.Start();

            Thread invokeThread = new Thread(new ThreadStart(Method));
            invokeThread.Start();            
        }

        private delegate void outputDelegate(string msg);

        /**对比TestThread与Method的区别:
         * for循环放在Test中会有卡顿问题;放在Method中不会有问题;
         * */

        private void TestThread()
        {
            for (int i = 0; i < 1000; i  )
            {
                Console.Write(""   i);
                Thread.Sleep(10);
                Console.WriteLine();

                this.Dispatcher.BeginInvoke(new outputDelegate(outputAction), i.ToString());
            }
        }

        private void outputAction(string msg)
        {
            this.textBox2.AppendText(msg);
            this.textBox2.AppendText("\n");
        }

        private void Method()
        {           
            this.Dispatcher.BeginInvoke(new InvokeDelegate(Test));            
        }

        private void Test()
        {   
            for (int i = 0; i < 1; i  )
            {
                Thread.Sleep(10);    
                textBox1.Text  = "1";
                Console.Write("==========================\r\n");
            }
        }

        private delegate void InvokeDelegate();       
    }
}