分享

WPF BackgroundWorker

 牛人的尾巴 2015-12-01

WPF BackgroundWorker

(2013-01-31 08:55:16)
            private BackgroundWorker backgroundWorker;

            public MainWindow()
            {
                InitializeComponent();

                int mainThreadID = Thread.CurrentThread.ManagedThreadId;
                Console.WriteLine("主线程ID:" + mainThreadID);

                backgroundWorker = new BackgroundWorker();
                backgroundWorker.WorkerReportsProgress = true;
                backgroundWorker.WorkerSupportsCancellation = true;
                backgroundWorker.ProgressChanged += BackgroundWorker_ProgressChanged;
                backgroundWorker.RunWorkerCompleted += BackgroundWorker_RunWorkerCompleted;
                backgroundWorker.DoWork += BackgroundWorker_DoWork;
            }

            void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
            {
                int backgroundWokrdThreadID = Thread.CurrentThread.ManagedThreadId;
                Console.WriteLine("DoWork BackgroundWorker线程ID:" + backgroundWokrdThreadID);

                int count = 0;
                //while (true)
                while (count<10)
                {
                    if (backgroundWorker.CancellationPending)
                        return;

                    count++;
                    backgroundWorker.ReportProgress(count * 10, string.Format("当前进度:{0} %", count * 10));
                    Dispatcher.BeginInvoke(new Action(() =>
                    {
                        this.showTB.Text = string.Format("当前进度:{0} %", count * 10);
                    }), 
                    DispatcherPriority.Normal, null);

                }
            }

            void BackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
            {
                int backgroundWokrdThreadID = Thread.CurrentThread.ManagedThreadId;
                Console.WriteLine("RunWorker CompletedBackgroundWorker线程ID:" + backgroundWokrdThreadID);
            }

            void BackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
            {
                int backgroundWokrdThreadID = Thread.CurrentThread.ManagedThreadId;
                Console.WriteLine("ProgressChanged BackgroundWorker线程ID:" + backgroundWokrdThreadID);

                int progressValue = e.ProgressPercentage;
                
                Console.WriteLine(string.Format("当前进度:{0} %", progressValue));
            }

            private void StartBackgroundWorker_Click(object sender, RoutedEventArgs e)
            {
                backgroundWorker.RunWorkerAsync();
                
            }
            private void CancelBackgroundWorker_Click(object sender, RoutedEventArgs e)
            {
                backgroundWorker.CancelAsync();
                backgroundWorker.Dispose();
            }


    <Grid Background="Black">
        <Button Content="StartBackgroundWorker" HorizontalAlignment="Left" Margin="29,29,0,0" VerticalAlignment="Top" Width="154" Height="33" Click="StartBackgroundWorker_Click"/>
        <TextBlock Name="showTB" HorizontalAlignment="Left" Margin="29,82,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="165" Width="391"
                   Background="OrangeRed" Foreground="White" FontSize="24" FontWeight="Bold"/>
        <Button Content="CancelBackgroundWorker" HorizontalAlignment="Left" Margin="261,29,0,0" VerticalAlignment="Top" Width="159" Height="33" Click="CancelBackgroundWorker_Click"/>
    </Grid>

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多