スレッドviewSource.Filterで処理されるメソッドでフィルタリング機能をラップできます。フィルタリングBackgroundWorkerする の数を決定できる場合は、開始カウントと組み合わせて使用できる をインクリメントして、進行状況を提供できます。objectscounter
編集:
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Threading;
using System.Windows;
using System.Windows.Data;
using System.Windows.Threading;
public partial class MainWindow : Window
{
private readonly Random random = new Random();
private BackgroundWorker backgroundWorker;
private ObservableCollection<int> CollectionOfInts { get; set; }
private ICollectionView ViewSource { get; set; }
public MainWindow()
{
InitializeComponent();
this.CollectionOfInts = new ObservableCollection<int>();
var nextRandom = this.random.Next(1, 200);
for (var i = 0; i <= nextRandom + 2; i++)
{
this.CollectionOfInts.Add(this.random.Next(0, 2000));
}
this.ViewSource = CollectionViewSource.GetDefaultView(this.CollectionOfInts);
this.ProgressBar.Maximum = this.CollectionOfInts.Count;
}
private void RunFilter()
{
this.ViewSource.Filter = LongRunningFilter;
}
private bool LongRunningFilter(object obj)
{
try
{
Application.Current.Dispatcher.BeginInvoke(
DispatcherPriority.Normal,
new Action(() => this.ProgressBar.Value++)
);
var value = (int) obj;
Thread.Sleep(3000);
return (value > 5 && value < 499);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return false;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
try
{
this.ProgressBar.Value = 0;
this.backgroundWorker = new BackgroundWorker();
this.backgroundWorker.DoWork += delegate { RunFilter(); };
this.backgroundWorker.RunWorkerAsync();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
objectsここでの基本原則は、フィルタリングする必要がある数 ( )を知っているthis.CollectionOfInts.Countため、これが私のMaximum値 (100%) であるということです。BackgroundWorkerスレッドでフィルタリングを開始します。BackgroundWorkerwith を開始すると、実際にフィルタリングを実行するメソッドが呼び出さRunWorkerAsyncれます (時間のかかるフィルターをシミュレートするためにそこに を配置しました)。 内のオブジェクトごとに 1 回呼び出されるため、現在どの反復が行われているかを通知するために使用できます。これを既知のものと組み合わせて使用すると、ある種の進歩が得られます。RunFilterLongRunningFilterThread.SleepLongRunningFilterViewSourceincrementcounterMaximum
これは実際の問題がどのように機能するかではありませんが、概念を示しています。