none
UI is frozen while running Task

    Question

  • 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. If I need a surtain order I will use 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;
    pairsResultsDataGrid.SelectedIndex = 0;
    
    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.Exclamation.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.
     
    TestPairSerial(pair) method has the following structure:
    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
    
                    } while (isSetbCompoundParams); 
     
                } while (isSetaCompoundParams); 
     
                      //filling up results into one table and two dictionaries (main window variables)
            }
    
     

    Vitaly


    • Edited by VitN Tuesday, December 27, 2011 7:47 PM
    Tuesday, December 27, 2011 5:29 PM

Answers

  • Yes.  Anything bound to the View is going to require marshaling back to the UI thread.  You should consider reworkign this so that the analytical portion happens on the background thread, and then you can update your collections at the end on the UI thread.

     

     


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by VitN Wednesday, December 28, 2011 3:32 PM
    Wednesday, December 28, 2011 3:00 AM

All replies

  • You can't have multiple threads writing into a List or ObservableCollection - though you can read from them.

     

    Also, if you're data binding to an ObservableCollection<T>, you must have the writes always come from the UI thread.

     

    " But then my application is still frozen untill ContinueWhenAll is done."

     

    This is difficult to figure out, based on the limited code you're showing.  My suspicion is that somethign in your method calls is using Dispatcher.Invoke or Control.Invoke, which in turn marshals back to teh UI thread, and effectively keeps it locked up during the entire operation.

     


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Tuesday, December 27, 2011 8:50 PM
  • Unfortunately the code is too long and calling multiple methods. None of them uses Dispatcher.Invoke or Control.Invoke. This method I used before I started thinking about UI responsiveness so nothing in it calls any threads and everything was done on UI thread. Later I've just wraped the call into a Task:

     Task task = Task.Factory.StartNew(() => TestPairSerial(pair));

    TestPairSerial(pair) communicates with properties and fields of MainWindow:

    1.  property: minDataCollection (ObservableCollection<T>) - only reading from it

    2. field: pairTestResultsTable (DataTable) - writes into it

    3. field: pairsPoints (Dictionary) - writes into it

    4. field: pairsParams (Dictionary) - writes into it

    5. field: passes (int) - increments it by 1 with every pass

    minDataCollection is bound to the Chart control (in XAML)

    Could any of the above cause application irresponsiveness?

    Thank you for your help.


    Vitaly
    Wednesday, December 28, 2011 2:18 AM
  • Yes.  Anything bound to the View is going to require marshaling back to the UI thread.  You should consider reworkign this so that the analytical portion happens on the background thread, and then you can update your collections at the end on the UI thread.

     

     


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by VitN Wednesday, December 28, 2011 3:32 PM
    Wednesday, December 28, 2011 3:00 AM