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();
            }



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







    附件列表

    • 相关阅读:
      JAVA 程序pending 数据库锁
      圆形相遇问题
      DROP TABLE
      sed 删除
      JVM程序计数器
      《神奇的数学》颠覆你对数学的初始感觉
      error "OPatch cannot find a valid oraInst.loc file to locate Central Inventory
      【linux】提醒"libc.so.6: version `GLIBC_2.14' not found"系统的glibc版本太低
      Linux 常用命令随笔(一)
      国际结算业务--国际结算中的票据
    • 原文地址:https://www.cnblogs.com/xe2011/p/3761572.html
    Copyright © 2011-2022 走看看