在 WPF中使用 BackgroundWorker
<div id="cnblogs_post_body">http://pic002.cnblogs.com/images/2012/315059/2012081615092818.jpgXAML:
<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();
}
}
}
页:
[1]