六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 188|回复: 0

在 WPF中使用 BackgroundWorker

[复制链接]

升级  16%

20

主题

20

主题

20

主题

秀才

Rank: 2

积分
74
 楼主| 发表于 2012-12-10 14:07:05 | 显示全部楼层 |阅读模式
<div id="cnblogs_post_body">
XAML:
<Window x:Class="WPFClientWCF.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="430" Width="863">
    <Grid>
        <Button Content="Start" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/>
        <DataGrid HorizontalAlignment="Left" Name="dataGridControl" Margin="0,69,0,0" VerticalAlignment="Top" Height="313" Width="847"/>
        <ProgressBar HorizontalAlignment="Left" Height="22" Margin="99,10,0,0" Name="progressbar1" VerticalAlignment="Top" Width="567"/>
        <Label Content="0" HorizontalAlignment="Left" Margin="685,10,0,0" Name="txtLab" VerticalAlignment="Top" RenderTransformOrigin="0.4,0.115"/>
        <Label Content="" HorizontalAlignment="Left" Name="txtLabCount" Margin="10,38,0,0" VerticalAlignment="Top"/>
    </Grid>
</Window>
Window.CS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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.Navigation;
using System.Windows.Shapes;
using System.Windows.Forms;
namespace WPFClientWCF
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        List<ServiceReferenceQuery.DCPersonLoc> listInfo;
        System.ComponentModel.BackgroundWorker backgroundWorker1;
        public MainWindow()
        {
            InitializeComponent();
            backgroundWorker1 = new System.ComponentModel.BackgroundWorker();
            progressbar1.Maximum = 100;
            backgroundWorker1.WorkerReportsProgress = true;
            backgroundWorker1.WorkerSupportsCancellation = true;
            backgroundWorker1.ProgressChanged += backgroundWorker1_ProgressChanged;
            backgroundWorker1.RunWorkerCompleted += backgroundWorker1_RunWorkerCompleted;
            backgroundWorker1.DoWork += backgroundWorker1_DoWork;
                }
        void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            DoWork(backgroundWorker1);
        }
        private void DoWork(System.ComponentModel.BackgroundWorker bk)
        {
            listInfo = new List<ServiceReferenceQuery.DCPersonLoc>();
            ServiceReferenceQuery.ServiceGeneralQueryClient query = new ServiceReferenceQuery.ServiceGeneralQueryClient();
            for (int i = 0; i <= 100; i++)
            {
                if (bk.CancellationPending)
                {
                    return;
                }
             listInfo=query.GetPathPersonLoc("F00015", DateTime.Now.AddHours(-5), DateTime.Now, 1);
             bk.ReportProgress(i,string.Format("当前正在查询进度:{0} %",i));
            }
        }
        void backgroundWorker1_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                System.Windows.MessageBox.Show("OK");
                dataGridControl.ItemsSource = listInfo;
                txtLabCount.Content ="查到数据:"+ listInfo.Count.ToString();
            }
            else if (e.Error != null)
            {
                System.Windows.MessageBox.Show(e.Error.ToString());
            }
        }
        void backgroundWorker1_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
        {
            progressbar1.Value = e.ProgressPercentage;
            txtLab.Content = e.UserState.ToString();
        }
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            if (backgroundWorker1.IsBusy)
            {
                return;
            }
            backgroundWorker1.RunWorkerAsync();
        }
    }
}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表