码迷,mamicode.com
首页 > 其他好文 > 详细

BackgroundWorker 组件 -- 进度条

时间:2018-08-29 16:59:29      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:round   进度   one   cancel   hid   cli   event   hide   mys   

 

技术分享图片

 

代码:

技术分享图片
 BackgroundWorker bw = new BackgroundWorker();
        public MainWindow()
        {
            InitializeComponent();
            
            bw.WorkerReportsProgress = true;
            bw.WorkerSupportsCancellation = true;
            bw.DoWork += bw_DoWork;
            bw.ProgressChanged += bw_ProgressChanged;
            bw.RunWorkerCompleted += bw_RunWorkerCompleted;
            
            
        }

        void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.txtMsg.Text = "下载完成";
        }

        void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            this.mySlider.Value = (double)e.ProgressPercentage / 10;
            this.txtMsg.Text = ((double)e.ProgressPercentage / 10).ToString();
           
        }

        void bw_DoWork(object sender, DoWorkEventArgs e)
        {
            for (int i = 1; i < 100; i++)
            {
                if (bw.CancellationPending == true)
                {
                    e.Cancel = true;
                    return;
                }

                bw.ReportProgress(i);
                Thread.Sleep(TimeSpan.FromSeconds(1));

            }

        }

        private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            bw.RunWorkerAsync();
            
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            bw.CancelAsync();
        }
View Code

 

BackgroundWorker 组件 -- 进度条

标签:round   进度   one   cancel   hid   cli   event   hide   mys   

原文地址:https://www.cnblogs.com/sportdog/p/9555120.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!