欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

WPF 精修篇 BackgroundWorker

程序员文章站 2024-02-16 12:29:52
...

效果

WPF 精修篇 BackgroundWorker

 

   <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="22*"/>
            <RowDefinition Height="11*"/>
            <RowDefinition Height="47*"/>
        </Grid.RowDefinitions>
        <StackPanel Orientation="Horizontal" Margin="0,28" VerticalAlignment="Center">
            <Label>开始数据</Label>
            <TextBox x:Name="beginText" HorizontalAlignment="Left" Height="31" TextWrapping="Wrap" Text="100" VerticalAlignment="Top" Width="100"/>
            <Label>结束数据</Label>
            <TextBox x:Name="endText" HorizontalAlignment="Left" Height="31" TextWrapping="Wrap" Text="1000000000" VerticalAlignment="Top" Width="100"/>
            <Button x:Name="button" Content="开始" HorizontalAlignment="Center" VerticalAlignment="Center" Width="75" Click="Button_Click"/>
            <Button x:Name="Cancel" Content="取消" HorizontalAlignment="Center" VerticalAlignment="Center" Width="75" Click="Cancel_Click"/>
        </StackPanel>
        <StackPanel Margin="0" Grid.Row="1" Grid.RowSpan="2">
            <TextBlock x:Name="odd" TextWrapping="Wrap"  Text="奇数数量:"/>
            <TextBlock x:Name="even" TextWrapping="Wrap" Text="偶数数量:"/>
            <ProgressBar x:Name="Prebar" Height="20"/>
        </StackPanel>
    </Grid>
      private int oddcount =0;
        private int evencount =0;
        public void Make(int from ,int to)
        {
            oddcount = 0;
            evencount = 0;
            int num = (to - from) / 100;
            for (int i = from; i < to; i++)
            {
                if (worker.CancellationPending == true) 
                {
                    return;
                }
              
                if (i % 2 == 0)
                {
                    evencount++;
                }
                else 
                {
                    oddcount++;
                }
                if (i % num == 0 && (worker != null) && (worker.WorkerReportsProgress = true)) 
                {
                    worker.ReportProgress(i / num);
                }
            }
           
        }
        BackgroundWorker worker = null;
        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;
                            odd.Text = "奇数数量:0" ;
                           even.Text = "偶数数量:0" ;

                  worker = new BackgroundWorker();
                //异步取消 需要增加这个 不然取消失效
                  worker.WorkerSupportsCancellation = true;
                //支持报告进度
                  worker.WorkerReportsProgress = true;
                //注册滚动条事件
                worker.ProgressChanged+=worker_ProgressChanged;
               
                //注册任务
                  worker.DoWork+=worker_DoWork;
                  //任务完毕触发
                  worker.RunWorkerCompleted += worker_RunWorkerCompleted;
                //给任务传参
                  worker.RunWorkerAsync(new Tuple<int, int>(from, to));

            }

        }

        private void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            Prebar.Value = e.ProgressPercentage;
        }

        void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
          //  throw new NotImplementedException();
            if (sender is BackgroundWorker) 
            {

                if (e.Cancelled == true)
                {
                    odd.Text = "任务已取消";
                    even.Text = "";
                }
                else 
                {
                    odd.Text = "奇数数量:" + oddcount;
                    even.Text = "偶数数量:" + evencount;
                }
                BackgroundWorker k = (BackgroundWorker)sender;
                k.DoWork -= worker_DoWork;
                k.RunWorkerCompleted -= worker_RunWorkerCompleted;
                k = null;
                button.IsEnabled = true;
            }
       
        }

        private void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            var args = ( Tuple<int, int>)e.Argument;
            Make(args.Item1, args.Item2);
            if (worker.CancellationPending == true) 
            {
                e.Cancel = true;
            }
        }

         
        private void Cancel_Click(object sender, RoutedEventArgs e)
        {

            worker.CancelAsync();
        }

 

相关标签: WPF