WPF 精修篇 非UI进程后台更新UI进程

WPF 精修篇 非UI进程后台更新UI进程_第1张图片

 

 
        
            
            
        
        
            
            
            
            
            

 

       private int oddcount =0;
        private int evencount =0;
        public void Make(int from ,int to) 
        {
            for (int i = from; i < to; i++)
            {
                if (i % 2 == 0)
                {
                    evencount++;
                }
                else 
                {
                    oddcount++;
                }
            }
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            int from=0;
            int to = 0;
            if(int.TryParse(beginText.Text,out from)&&int.TryParse(endText.Text,out to) )
            {
                button.IsEnabled = false;
                ThreadPool.QueueUserWorkItem(_ =>
                {
                    Make(from, to);
                    Dispatcher.BeginInvoke(new Action(() =>
                    {
                        odd.Text = "奇数数量:" + oddcount;
                        even.Text = "偶数数量:" + evencount;
                        button.IsEnabled = true;
                    }));
                  
                });
        
            }

        }

 

   Dispatcher.BeginInvoke(new Action(() =>
                    {
                       //UI线程
                    }));

 

你可能感兴趣的:(WPF)