1-将以下代码复制并粘贴到 MainWindow.xaml 文件中。
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Grid>
<DataGrid x:Name="DataGrid1"/>
</Grid>
</Window>
2-将以下代码复制并粘贴到 MainWindow.xaml.cs 文件中。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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.ComponentModel;
using System.Threading;
namespace WpfApplication1
{
public partial class MainWindow : Window
{
BackgroundWorker BackgroundWorker1 = new BackgroundWorker();
BackgroundWorker BackgroundWorker2 = new BackgroundWorker();
System.Data.DataTable DataTable1 = new System.Data.DataTable();
public MainWindow()
{
InitializeComponent();
BackgroundWorker1.DoWork += BackgroundWorker1_DoWork;
BackgroundWorker2.DoWork += BackgroundWorker2_DoWork;
}
void Window_Loaded(object sender,RoutedEventArgs e)
{
BackgroundWorker1.RunWorkerAsync();
BackgroundWorker2.RunWorkerAsync();
}
private void BackgroundWorker1_DoWork(System.Object sender,System.ComponentModel.DoWorkEventArgs e)
{
Dispatcher.Invoke(() =>
{
Window1 myWindow1 = new Window1();
myWindow1.ShowDialog();
});
}
private void BackgroundWorker2_DoWork(System.Object sender,System.ComponentModel.DoWorkEventArgs e)
{
for (int i = 1; i <= 7; i++)
DataTable1.Columns.Add();
for (int i = 1; i <= 1048576; i++)
DataTable1.Rows.Add(i);
Dispatcher.Invoke(() =>
{
DataGrid1.ItemsSource = DataTable1.DefaultView;
});
}
}
}
3-创建一个新窗口,并将其命名为 Window1 。
4-将以下代码复制并粘贴到 Window1.xaml 文件中。
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="1000" ContentRendered="Window_ContentRendered">
<Grid>
<ProgressBar x:Name="ProgressBar1" Height="25" Width="850"/>
</Grid>
</Window>
5-将以下代码复制并粘贴到 Window1.xaml.cs 文件中。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 WpfApplication1
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void Window_ContentRendered(object sender,EventArgs e)
{
ProgressBar1.IsIndeterminate = true;
}
}
}
6-当您运行此项目时,您会看到 ProgressBar1 冻结了两三秒,而下一行的运行是由于向DataGrid添加了1048576行。 (大行)
DataGrid1.ItemsSource = DataTable1.DefaultView;
我不希望 ProgressBar1 被冻结。
那为什么 BackgroundWorker 无法阻止 ProgressBar 冻结?