CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2009

    Threading Problem with Custom Class used in XAML (no DispatcherObject)

    Hi everyone,

    I'm pretty new to WPF. I am using VS 2008 with .NET 3.5 SP1.
    Here's what's up and running:
    A small audio player with ElementFlow (from Fluidkit) to display the artworks stored in the MP3s (all from Jamendo: creative commons license, neatly tagged and artwork included --> no seperate image file).
    I am planning on using the with Phidgets components. This is why I implemented this structure:

    - Window1.xaml (and, of course, Window1.xaml.cs) hold all GUI objects, that is, ElementFlow, some labels, buttons and a ListBox.
    - Player.cs which is supposed to be the main logic. It is responsible to manage playlists, tracks, nextSong, play, pause, stop, trackTime and whatsoever.

    When Window1 loads, it initiates Player.cs. The player object loads the configuration as well as the default playlist. To avoid that threading thing I dispatch custom events for i.e. loading a playlist, switching to next song, etc. Then there is a listener in the Window1.xaml.cs which responds to the events dispatched in Player.cs. It updates the GUI components (like ListBox [for playlist depiction] and ElementFlow).

    And now here comes the problem:
    When I instantiate a PhidgetInterfaceKit-class (it responds to an i.e. physical button push) I want it to be able to call methods in the player class. This caused a lot of exceptions in the beginning in the Window1.xaml.cs (Thread not owner or something like that). I managed to get a workaround via surrounding all GUI component updates with
    playlist.Dispatcher.Invoke(DispatcherPriority.Normal, (CrossThreadDelegate)delegate { playlist.SelectedIndex = player.indexOf(e.getSong); });
    for example. This seems to actually work.
    BUT I had to create a custom Class called CoverCollection (based on the ElementFlow's version of StringCollection) to act as a datasource via databinding (done in the XAML-file). This is due to the fact that I don't have image files of the MP3s' artwork but rather load it dynamically as a BitmapImage. CoverCollection.cs looks like this:
    class CoverCollection : ObservableCollection<BitmapImage>
    This allows me to add BitmapImages which are automatically bound to ElementFlow. Finally, the problem:
    I cannot use Dispatcher.Invoke for CoverCollection, but, of course, get the exception. Does anyone have a workaround on this?
    Furthermore, I find my solution using Dispatcher.Invoke on all GUI components quite ugly. I'm sure there is a much better solution.
    I am deeply sorry for asking these stupid questions but this is my 1st WPF/C# application ever and time is getting short...

    Thanks to anyone in advance,

    P.S. I can provide more code if neccessary

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004

    Re: Threading Problem with Custom Class used in XAML (no DispatcherObject)

    Look around for a thread-safe ObservableCollection. You can find one implementation here:


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.