可能是我错了,但我的推测是任何后台线程都可以读取和写入List或ObservableCollection,如果我不关心任何特定的顺序。如果我需要一个确定的顺序,我将使用BlockingCollection。用户界面在运行时被冻结任务
private void buttonTesting_Click(object sender, RoutedEventArgs e)
{
PrepareDataForTesting();
Stopwatch timer1 = new Stopwatch();
timer1.Start();
//some code preparing data
List<Task> tasks = new List<Task>();
//Testing for each pair
foreach (InterfaceWithClassName aCompound in Group1)
{
foreach (InterfaceWithClassName bCompound in Group2)
{
InstancePair pair = new InstancePair();
//some code
Task task = Task.Factory.StartNew(() => TestPairSerial(pair));
tasks.Add(task);
}
}
var ui = TaskScheduler.FromCurrentSynchronizationContext();
Task.Factory.ContinueWhenAll(tasks.ToArray(),
antecedents =>
{
timer1.Stop();
TimeSpan ts1 = timer1.Elapsed;
string elapsedTime1 = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts1.Hours, ts1.Minutes, ts1.Seconds, ts1.Milliseconds/10);
statusLabel_1.Content = "Elapsed time to run the test" + elapsedTime1;
statusLabel_0.Content = "Testing made " + passes + " passes";
pairsResultsDataGrid.ItemsSource = pairsResultsTable.DefaultView;
System.Media.SystemSounds.Exclamation.Play();
}, CancellationToken.None, TaskContinuationOptions.None, ui);
System.Media.SystemSounds.Beep.Play();
}
(注:我不知道,如果它的事项 - “对”通过反思发现) 当我点击按钮,我能听到的最后一行 - System.Media.SystemSounds.Beep.Play() ;这意味着我们脱离了事件处理程序并启动了所有线程。但是,然后我的应用程序仍然冻结,直到ContinueWhenAll完成。 TestPairSerial(对)方法具有如下结构:
private void TestPairSerial(object instances)
{
do
{
do
{
//here are two methods that read data from minData ObservableCollection
//minData is a public static property of MainWindow
//minData is bound to Chart control (in XAML)
} while (isSetbCompoundParams);
} while (isSetaCompoundParams);
//filling up results into one table and two dictionaries (main window variables)
}
你可以显示你的方法的其余部分?当UI被冻结时,你也可以尝试暂停执行,并查看mainthread停止的位置。 – 2011-12-28 15:48:33
我试图暂停,是的它是在多个方法之一(TestPairSerial内)。对不起,有太多的代码和太多的代码显示(我尽我所能展示了本质)。 – Vitaly 2011-12-28 15:59:30
我试图在MSDN论坛上提出同样的问题。看起来他们的答案是有道理的。 http://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/74b2c600-83ad-4890-b2b2-27d8f07f5a90 – Vitaly 2011-12-28 16:02:25