关闭

C#的BackgroundWorker

517人阅读 评论(0) 收藏 举报
分类:
using log4net;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Cat8637pos.Dialogs
{
    /// <summary>
    /// Interaction logic for DlgDataDownload.xaml
    /// </summary>
    public partial class DlgDataDownload : Window
    {
        private static readonly ILog logger =
            LogManager.GetLogger(typeof(DlgDataDownload));

        private BackgroundWorker m_BackgroundWorker;// 申明后台对象

        public DlgDataDownload()
        {
            InitializeComponent();

            m_BackgroundWorker = new BackgroundWorker(); // 实例化后台对象

            m_BackgroundWorker.WorkerReportsProgress = true; // 设置可以通告进度
            m_BackgroundWorker.WorkerSupportsCancellation = true; // 设置可以取消

            m_BackgroundWorker.DoWork += new DoWorkEventHandler(DoWork);
            m_BackgroundWorker.ProgressChanged += new ProgressChangedEventHandler(UpdateProgress);
            m_BackgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(CompletedWork);

            m_BackgroundWorker.RunWorkerAsync(this);
        }

        protected void SetProgress(int value)
        {
            ProgressBar1.Dispatcher.Invoke(new Action(() =>
            {
                ProgressBar1.Value = value;
            }));
        }

        void DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker bw = sender as BackgroundWorker;
            MainWindow win = e.Argument as MainWindow;

            try
            {
                int i = 0;
                while (i <= 100)
                {
                    if (bw.CancellationPending)
                    {
                        e.Cancel = true;
                        break;
                    }

                    i++;
                    bw.ReportProgress(i++);

                    Thread.Sleep(1000);
                }
            }catch(Exception  ex)
            {
                logger.Error(ex.Message);
            }
        }

        void UpdateProgress(object sender, ProgressChangedEventArgs e)
        {
            SetProgress(e.ProgressPercentage);
        }

        void CompletedWork(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                MessageBox.Show("Error");
            }
            else if (e.Cancelled)
            {
                MessageBox.Show("Canceled");
            }
            else
            {
                MessageBox.Show("Completed");
            }
        }

        //取消当前background task
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            m_BackgroundWorker.CancelAsync();
        }

        //拖动当前窗口
        private void Grid_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
                DragMove();
        }
    }
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1002575次
    • 积分:13324
    • 等级:
    • 排名:第1057名
    • 原创:326篇
    • 转载:35篇
    • 译文:1篇
    • 评论:190条
    文章分类
    文章存档
    最新评论