2011-12-27 58 views
1

可能是我错了,但我的推测是任何后台线程都可以读取和写入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) 
    } 
+0

你可以显示你的方法的其余部分?当UI被冻结时,你也可以尝试暂停执行,并查看mainthread停止的位置。 – 2011-12-28 15:48:33

+0

我试图暂停,是的它是在多个方法之一(TestPairSerial内)。对不起,有太多的代码和太多的代码显示(我尽我所能展示了本质)。 – Vitaly 2011-12-28 15:59:30

+0

我试图在MSDN论坛上提出同样的问题。看起来他们的答案是有道理的。 http://social.msdn.microsoft.com/Forums/en-US/parallelextensions/thread/74b2c600-83ad-4890-b2b2-27d8f07f5a90 – Vitaly 2011-12-28 16:02:25

回答

0

您正在执行在主线程的任务。你可以执行整个代码与Dispatcher.BeginInvoke

this.Dispatcher.BeginInvoke(new Action(() => { 
    // your code here 
}), null); 
+0

默认情况下,一旦任务启动,它就会异常运行。我以下面的博客为例http://blogs.msdn.com/b/csharpfaq/archive/2010/06/01/parallel-programming-in-net-framework-4-getting-started.aspx – Vitaly 2011-12-27 19:02:27

+0

这是真的,但其余的代码不会。在我建议的上面的代码块中包装你的整个代码并没有什么坏处。你可能会觉得它很有用。 – 2011-12-27 19:05:21

+0

对不起。我刚试过它没有用。结果相同。我的猜测是 - 原因是在mainWindow变量和异步线程之间经常进行通信。 – Vitaly 2011-12-27 19:20:25