This project is read-only.

Threading, Binding and Progress Updates

Dec 5, 2014 at 1:21 PM
Hi,

I love the MUI it looks great, I am porting some code across from Windows Forms and I am struggling with a couple of basic concepts. I am by no means new to coding but C# and WPF are reasonably new to me.

I have a Serial Port listener that listens to packets, parses them and updates a class with various parameters based on the contents of the packets.

I would like to know:

A) How best to run the serial listener ( Threading and Updates to MUI to be taken into consideration )
B) When I start the Listener I have a series of Text based messaged that I would like to show in the GUI but can't find a way to show these!
C) Where should I run the Listener from from in the basic MUI framework ( Main or a User Control or Somewhere Else )
D) Once the listener has started I want to bind the MUI fields ( Text Boxes and Labels etc ) to the data in the class, where is the best place to host the class and how do I bind to the data.

This may seem simple to the experts here but I am getting swamped with information and struggling to wade through the jungle!!

Code snippets much appreciated - and for me ... No answer is too simple, I am happy to be taught to suck eggs!!

Looking forward to your valued support!

Andy
Dec 13, 2014 at 11:32 AM
My approach would be to create a ViewModel where you put all the data that you want to display in the UI. Next to that create a Model that includes the serial listener producing the data. The listener typically operates on a background thread and reports back to the view model (likely using standard .NET events).

The problem with updating the UI based on messages originating from a background thread, is that the UI can only be updated on the UI thread. You do need to perform some kind of thread marshalling. Dispatcher.Invoke is your friend here. For more info see http://msdn.microsoft.com/en-us/library/ms741870%28v=vs.110%29.aspx

Your ViewModel contains a reference to the Model. Assign the ViewModel to the MainWindow's DataContext and setup the bindings to display the data. You could use an MVVM framework such as MVVMLight, or create your own. I'm a huge fan of MEF, and have created a small tutorial on how to use it in MUI.

Good luck!