zoukankan      html  css  js  c++  java
  • WPF 多线程




    写法3
           private void button1_Click(object senderRoutedEventArgs e)
           {
                System.Threading.Thread thread1 = new System.Threading.Thread(() =>
                {
                   //过程
                });
                thread1.Start();
           }

    使用控件时会引起 其他信息: 由于其他线程拥有此对象,因此调用线程无法对其进行访问。

    写法2  一般采用这种写法

            private void DoWork()
            {
                for (int i = 0; i < 500; i++)
                {
                    this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normalnew Action(delegate()
                    {
                        //在这里执行代码
                        button1.Content = Convert.ToString(i);
                    }));
                    System.Threading.Thread.Sleep(10);
                }
            }
     
            private void Button_Click(object senderRoutedEventArgs e)
            {
                System.Threading.Thread thread1 = new System.Threading.Thread(new System.Threading.ThreadStart(DoWork));
                thread1.Start();
            }



    写法1 


            private void Button_Click_1(object senderRoutedEventArgs e)
            {
                System.Threading.Thread thread1 = new System.Threading.Thread(
                    new System.Threading.ThreadStart(
                                delegate()
                                {
                                    for (int i = 0; i < 500; i++)
                                    {
                                        this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normalnew Action(delegate()
                                            {
                                                //your code herer...
                                                button1.Content = Convert.ToString(i);
                                            }));
                                        System.Threading.Thread.Sleep(10);
                                    }
                                }
                    ));
                thread1.Start();
            }



    使用控件时会引起 其他信息: 由于其他线程拥有此对象,因此调用线程无法对其进行访问。







    附件列表

    • 相关阅读:
      转MySQL详解--索引
      [转]关于MYSQL Innodb 锁行还是锁表
      根据STATUS信息对MySQL进行优化
      源码编译安装 MySQL 5.5.x 实践
      thrift服务端到客户端开发简单示例
      thrift安装
      thrift 调取 python php go 客户端代码
      Linux系统启动级别及顺序
      Linux手动释放内存
      /etc/passwd&/etc/shadow文件分析
    • 原文地址:https://www.cnblogs.com/xe2011/p/3761572.html
    Copyright © 2011-2022 走看看