简体   繁体   English

UI在运行Task时冻结

[英]UI is frozen while running Task

May be I am wrong but my assuption is that any background thread can read and write into List or ObservableCollection if I don't care about any particular order. 可能是我错了,但我的假设是,如果我不关心任何特定顺序,则任何后台线程都可以读写List或ObservableCollection。 If I need a surtain order I will use BlockingCollection. 如果需要补充订单,我将使用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();               
            }

(Note: I am not sure if it matters - "pair" is found through Reflection) When I click the button I can hear the last line - System.Media.SystemSounds.Beep.Play(); (注意:我不确定这是否重要-通过反射找到“对”)单击按钮时,我可以听到最后一行-System.Media.SystemSounds.Beep.Play(); meaning we out of the event handler and all the threads are launched. 这意味着我们退出了事件处理程序,并且所有线程都已启动。 But then my application is still frozen untill ContinueWhenAll is done. 但是然后我的应用程序仍然冻结,直到ContinueWhenAll完成。 TestPairSerial(pair) method has the following structure: TestPairSerial(pair)方法具有以下结构:

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)
    }

You are executing the tasks in the main thread. 您正在执行主线程中的任务。 You can execute the whole code asynchronously with Dispatcher.BeginInvoke 您可以使用Dispatcher.BeginInvoke异步执行整个代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM