We help IT Professionals succeed at work.

Threads and UI under WPF

Hi Experts,

    I have to teach myself WPF in a hurry.  Here are my first very basic but broad questions!

1.   I want to be able to run some tasks in the background at all times (reading info from the serial port).  Where do I even instantiate and start my thread?  There is no more Program.cs...

2.  Once I read the serial port I want to populate a List of classes and have the UI feed off of it and display the info that the background thread read from the serial port.  Under WinForms, I'd just have a global reference to the same instance of my list and have the reader and writter threads lock it up and access it that way.  How would all of this look under WPF?  Just give me a brid's-eye view and I will research the specifics!  I am working with VS2010 and Framework 3.5 but I can up it to 4.0 if I really have to!

Thanks!
Comment
Watch Question

Research & Development Manager
Commented:
1.  Every WPF application has an App.xaml and MainWindow.xaml control, both of which have corresponding *.cs code behind files.  Unless you are looking to do something a bit more advanced, the general entry point into your WPF application will be the MainWindow.xaml.cs file.

For processing tasks in the background without causing your UI to hang, you either want to look at the WPF Dispatcher ( http://www.switchonthecode.com/tutorials/working-with-the-wpf-dispatcher ) or the BackgroundWorker ( http://www.switchonthecode.com/tutorials/csharp-tutorial-using-the-backgroundworker-class ).

2.  Both the Dispatcher and BackgroundWorker have "work completed" event handlers, that are fired after the "do work" events are completed.  In this event you can make a call to your List<T>.
To add to #1...

Program.cs (or, more specifically, the "main" entry point) still exists, VS just generates that automatically for the WPF project template.

App.xaml.cs defines an Application object. You can override the OnStartup event handler if you need to drop initialization code, or access command line arguments (the event args object "e" will have an Args property.)