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



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







附件列表

     

    posted @ 2014-05-30 17:46  XE2011  阅读(218)  评论(0编辑  收藏  举报