locked
How can I prevent deadLock in async?

    Question

  • Hi,
    How can I prevent deadLock in async?

    In ComboBoxCategories_SelectionChanged I have deadLock.

    I used ConfigureAwait(false).
    It don't wait even I used await.

    Please help me.

      private ObservableCollection<wordsGViewModel> WordsGS;

           public ObservableCollection<wordsGViewModel> wordsGS
           {
               get
               {
                   return WordsGS;
               }
               set
               {
                   WordsGS = value;
                   RaisePropertyChanged("wordsGS");
               }
           }

      public async Task<ObservableCollection<wordsGViewModel>> GetwordsGScategoryS(string category1)
           {
               wordsGS = new ObservableCollection<wordsGViewModel>();
               var db = new SQLite.SQLiteAsyncConnection(App.DBPath);

               var _wordsGS = await db.Table<wordsG>().Where(
                    p1 => p1.nameCategory == category1).OrderBy(
                    p2 => p2.nameCategory).ToListAsync().ConfigureAwait(false);

               foreach (var _wordsG in _wordsGS)
               {
                   var wordsG = new wordsGViewModel()
                   {
                       _id = _wordsG._id,
                       nameCategory = _wordsG.nameCategory,
                       nameWord1 = _wordsG.nameWord1,
                       nameWord2 = _wordsG.nameWord2,
                       nameWord3 = _wordsG.nameWord3,
                       nameWord4 = _wordsG.nameWord4,
                       nameWord5 = _wordsG.nameWord5,
                       nameWord6 = _wordsG.nameWord6,
                       nameWord7 = _wordsG.nameWord7,
                       nameWord8 = _wordsG.nameWord8,
                       nameWord9 = _wordsG.nameWord9,
                       nameWord10 = _wordsG.nameWord10
                   };

                   wordsGS.Add(wordsG);

               }
               return wordsGS;
           }

       public ObservableCollection<wordsGViewModel> GetwordsGScategory(string category1)
           {
               wordsGS = new ObservableCollection<wordsGViewModel>();
               using (var db = new SQLite.SQLiteConnection(App.DBPath))
               {
                   var query = db.Table<wordsG>().Where(
                        p1 => p1.nameCategory == category1).OrderBy(
                        p2 => p2.nameCategory).ToList();

                   foreach (var _wordsG in query)
                   {
                       var wordsG = new wordsGViewModel()
                       {
                           _id = _wordsG._id,
                           nameCategory = _wordsG.nameCategory,
                           nameWord1 = _wordsG.nameWord1,
                           nameWord2 = _wordsG.nameWord2,
                           nameWord3 = _wordsG.nameWord3,
                           nameWord4 = _wordsG.nameWord4,
                           nameWord5 = _wordsG.nameWord5,
                           nameWord6 = _wordsG.nameWord6,
                           nameWord7 = _wordsG.nameWord7,
                           nameWord8 = _wordsG.nameWord8,
                           nameWord9 = _wordsG.nameWord9,
                           nameWord10 = _wordsG.nameWord10
                       };
                       wordsGS.Add(wordsG);
                   }
               }
               return wordsGS;
           }

    }
     // private void newGameFirst(string selectedItem1)
    private async void newGameFirst(string selectedItem1)
            {
               wordsGSViewModel1 = new wordsGSViewModel();

               wordsGS1 = new ObservableCollection<wordsGViewModel>();
     
              //  wordsGS1 =  wordsGSViewModel1.GetwordsGScategory(selectedItem1);
               wordsGS1 = await wordsGSViewModel1.GetwordsGScategoryS(selectedItem1);
        writeInTBs(); 
           
    }

     private void ComboBoxCategories_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                if (comboBoxCategories.SelectedIndex == -1)//check if anything is selected
                    return; //do nothing if nothing is selected

                if (e.RemovedItems != null && e.RemovedItems.Count > 0)
                {
                    if (this.comboBoxCategories.SelectedItem != null)
                    {    
                        selectedItem =  this.comboBoxCategories.SelectedItem.ToString().Trim();
                        selectedCategory = listCategories.IndexOf(selectedItem);
                      
                       comboBoxCategories.SelectedIndex = selectedCategory;

                       roamingSettings = Windows.Storage.ApplicationData.Current.RoamingSettings;

                       roamingSettings.Values["MyValueSelectedIndexCategory"] = selectedCategory.ToString();
                       roamingSettings.Values["MyValueCategory"] = selectedItem.Trim();
                      
                        if (GridX.Children.Count() > 0)
                       {
                           removeChildren1();
                       }
                        loadTBs();
                      newGameFirst(selectedItem);
                    }
                }
            }
         private void removeChildren1()
            {
                int intTotalChildren = GridX.Children.Count - 1;
                for (int intCounter = intTotalChildren; intCounter >= 0; intCounter--)
                {
                    if (GridX.Children[intCounter].GetType() == typeof(TextBlock))
                    {
                        TextBlock ucCurrentChild = (TextBlock)GridX.Children[intCounter];
                        GridX.Children.Remove(ucCurrentChild);
                    }
                }
             }

        private void loadTBs()
        {
           // System.Diagnostics.Debug.WriteLine("In loadTBs: ");

           for (int i = 0; i < _maxRow; i++)
                {
                    for (int j = 0; j < _maxCol; j++)
                    {
                        TextBlock lbl = new TextBlock();

                        var tblock = lbl;

                        tblock.Name = "tb_" + i.ToString() + j.ToString();

                   //    tblock.Text = matrix[i, j].ToString();
                     //   tblock.Text = i.ToString() + j.ToString();
                        tblock.Width = 50; //Width

                        tblock.Height = 50;  //Height

                        tblock.VerticalAlignment = VerticalAlignment.Center;
                        tblock.VerticalAlignment = VerticalAlignment.Bottom;

                        tblock.TextWrapping = TextWrapping.Wrap;
                        tblock.Margin = new Windows.UI.Xaml.Thickness(0, -12, 0, -12);

                        tblock.TextAlignment = TextAlignment.Center;

                        tblock.HorizontalAlignment = HorizontalAlignment.Center;
                        tblock.FontSize = 24;
                        tblock.FontStyle = Windows.UI.Text.FontStyle.Normal;
                        tblock.FontWeight = Windows.UI.Text.FontWeights.Bold;

                        tblock.Foreground = _blackBrush; // new SolidColorBrush(Colors.Yellow);
                        tblock.TextWrapping = TextWrapping.Wrap;
               
                        tblock.IsTapEnabled = true;

                        Grid.SetRow(tblock, i);
                        Grid.SetColumn(tblock, j);

                        tblock.Tapped += new TappedEventHandler(tblock_Tapped);
                 
                       GridX.Children.Add(tblock);
             
                    }
                }
        }

      private void writeInTBs()
      {
         for (int i = 0; i < _maxRow; i++)
         {
              for (int j = 0; j < _maxCol; j++)
             {
                  object wantedNode = GridX.FindName("tb_" + i.ToString() + j.ToString());
     
               if (wantedNode is TextBlock)
                    {
                        TextBlock wantedChild = wantedNode as TextBlock;
                       wantedChild.Text = matrix[i, j].ToString();
              
                    }
              }


    ADRIAN DIBU

    Wednesday, June 18, 2014 3:48 PM

Answers

  • Hi,

    I cannot reproduce your problem. Would you mind sharing a sample which can reproduce the problem into OneDrive so that we can test it.

    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. <br/> Click <a href="http://support.microsoft.com/common/survey.aspx?showpage=1&scid=sw%3Ben%3B3559&theme=tech"> HERE</a> to participate the survey.

    • Marked as answer by adibu Thursday, July 3, 2014 1:59 AM
    Friday, June 27, 2014 6:38 AM

All replies

  • Hi,

    After seconds of waiting I had this msg

    ContextSwitchDeadlock occurred
    Message: Managed Debugging Assistant 'ContextSwitchDeadlock'

    when I try to change category and it is 2 words with 1 word is working.

    I removed all the Trim()

    Thanks

    Best regards


    ADRIAN DIBU

    Wednesday, June 18, 2014 5:55 PM
  • Hi,

    Even when is one word and it works if am doing it

    several time(changing Category) in a row I have DeadLock.

    It doesn't wait for finishing.

    What is wrong?

    Thanks


    ADRIAN DIBU

    Wednesday, June 18, 2014 6:15 PM
  • Hi,

    Is it a way to check if the SQLite finish the search and
    after to do a new search?
     Thanks


    ADRIAN DIBU

    Thursday, June 19, 2014 1:13 PM
  • Hi,

    I cannot reproduce your problem. Would you mind sharing a sample which can reproduce the problem into OneDrive so that we can test it.

    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. <br/> Click <a href="http://support.microsoft.com/common/survey.aspx?showpage=1&scid=sw%3Ben%3B3559&theme=tech"> HERE</a> to participate the survey.

    • Marked as answer by adibu Thursday, July 3, 2014 1:59 AM
    Friday, June 27, 2014 6:38 AM