Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Getting ObservableCollection ThreadSafe

Getting ObservableCollection ThreadSafe

Scheduled Pinned Locked Moved WPF
csharpwpfhelptutorialquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    ezazazel
    wrote on last edited by
    #1

    Hi guys! Experiencing something weird recently. Due to the fact, that my observable collection is the datacontext of my wpf app, I need to make sure, that every Thread is able to add Elements. Now the easy and working way is to add a

    Dispatcher.Invoke(new Action(()=>{collection.Add(element}));

    from every method being allowed to do so, which is kind of annoying. So I thought it would be a good idea to add the functionality to the observable collection itself. This is what I came up with:

    class MElementsCollection :ObservableCollection<MElement>
    {
    #region Variables
    private Dispatcher mainDipatcher;

        public Dispatcher MainDipatcher
        {
            get {
                if (mainDipatcher == null)
                    mainDipatcher = App.MainDispatcher;
                return mainDipatcher; 
            }
        }
        #endregion
    
        public void AddThreadSafe(MElement mElement)
        {
            if (Thread.CurrentThread != MainDipatcher.Thread)
            {                
                    MainDipatcher.Invoke(new Action(() =>
                        {
                            this.Add(mElement);
                        }));
                
            }
            else
                this.Add(mElement);
        }
    }
    

    Now what's actually happening is, that by adding many items short one after another, not every item is added to the collection but just for example every second one. Can anyone please help me with this? As always, Thank you in advance

    P 1 Reply Last reply
    0
    • E ezazazel

      Hi guys! Experiencing something weird recently. Due to the fact, that my observable collection is the datacontext of my wpf app, I need to make sure, that every Thread is able to add Elements. Now the easy and working way is to add a

      Dispatcher.Invoke(new Action(()=>{collection.Add(element}));

      from every method being allowed to do so, which is kind of annoying. So I thought it would be a good idea to add the functionality to the observable collection itself. This is what I came up with:

      class MElementsCollection :ObservableCollection<MElement>
      {
      #region Variables
      private Dispatcher mainDipatcher;

          public Dispatcher MainDipatcher
          {
              get {
                  if (mainDipatcher == null)
                      mainDipatcher = App.MainDispatcher;
                  return mainDipatcher; 
              }
          }
          #endregion
      
          public void AddThreadSafe(MElement mElement)
          {
              if (Thread.CurrentThread != MainDipatcher.Thread)
              {                
                      MainDipatcher.Invoke(new Action(() =>
                          {
                              this.Add(mElement);
                          }));
                  
              }
              else
                  this.Add(mElement);
          }
      }
      

      Now what's actually happening is, that by adding many items short one after another, not every item is added to the collection but just for example every second one. Can anyone please help me with this? As always, Thank you in advance

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      Why not take a look at Tamir's implementation here[^]? There's no need to reinvent the wheel here.

      "WPF has many lovers. It's a veritable porn star!" - Josh Smith

      As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

      My blog | My articles | MoXAML PowerToys | Onyx

      E 1 Reply Last reply
      0
      • P Pete OHanlon

        Why not take a look at Tamir's implementation here[^]? There's no need to reinvent the wheel here.

        "WPF has many lovers. It's a veritable porn star!" - Josh Smith

        As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

        My blog | My articles | MoXAML PowerToys | Onyx

        E Offline
        E Offline
        ezazazel
        wrote on last edited by
        #3

        Thank you!

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups