监测进展试图监视Parallel.ForEach环路我已经试过的建议提出this question可惜我已仍未能完成我想要的进步。在Parallel.ForEach
基本上我跑进当我试图执行的第一个问题建议(使用定时器)是,Parallel.ForEach方法是一个阻塞调用,因此计时器滴答回调没有发生。
所以我试着把Parallel.ForEach循环放在后台工作线程中。事实上允许发生计时器滴答事件,但直到ForEach操作完成后,我的计数器值才会更新。
这是代码的基本思想(带有背景工作)。
private StockList _StockListToProcess = null;
private static Int64 ItemsProcessed = 0;
private System.Windows.Threading.DispatcherTimer _timer = null;
private System.ComponentModel.BackgroundWorker _backWorker = null;
progressBar1.Minimum = 1;
progressBar1.Maximum = this._StockListToProcess.Count;
MainWindow.ItemsProcessed = 0;
this._timer = new System.Windows.Threading.DispatcherTimer();
this._timer.Interval = TimeSpan.FromMilliseconds(100);
this._timer.Tick += timer_Tick;
this._timer.Start();
this._backWorker = new System.ComponentModel.BackgroundWorker();
this._backWorker.DoWork += delegate(object o, System.ComponentModel.DoWorkEventArgs args)
{
Parallel.ForEach(this._StockListToProcess, new ParallelOptions() { MaxDegreeOfParallelism = 5 },
(Stock stock) =>
{
MyWebServiceClient serviceClient = new MyWebServiceClient();
MyWebServiceClient.ResponseEnum result = (MyWebServiceClient .ResponseEnum)serviceClient.SetProductPricing(token.LoginName, token.LoginPassword, token.SiteID.ToString(), stock.ProductCode, stock.ProductPrice);
System.Threading.Interlocked.Increment(ref MainWindow.ItemsProcessed);
});
this._timer.Stop();
};
private void timer_Tick(object sender, EventArgs e)
{
progressBar1.Value = MainWindow.ItemsProcessed;
}
我缺少什么?
任何解决方案的完整源代码? – Kiquenet 2012-08-22 10:59:41