New to MVVM - Best practices for seperating Data processing thread and UI Thread?

Posted by OffApps Cory on Stack Overflow See other posts from Stack Overflow or by OffApps Cory
Published on 2010-03-29T19:02:46Z Indexed on 2010/05/25 10:21 UTC
Read the original article Hit count: 621

Filed under:
|
|
|

Good day.

I have started messing around with the MVVP pattern, and I am having some problems with UI responsiveness versus data processing.

I have a program that tracks packages. Shipment and package entities are persisted in SQL database, and are displayed in a WPF view. Upon initial retrieval of the records, there is a noticeable pause before displaying the new shipments view, and I have not even implemented the code that counts shipments that are overdue/active yet (which will necessitate a tracking check via web service, and a lot of time).

I have built this with the Ocean framework, and all appears to be doing well, except when I first started my foray into multi-threading. It broke, and it appeared to break something in Ocean... Here is what I did:

Private QueryThread As New System.Threading.Thread(AddressOf GetShipments)


Public Sub New()
    ' Insert code required on object creation below this point.

    Me.New(ViewManagerService.CreateInstance, ViewModelUIService.CreateInstance)
    'Perform initial query of shipments
    'QueryThread.Start()
    GetShipments()
    Console.WriteLine(Me.Shipments.Count)
End Sub



Public Sub New(ByVal objIViewManagerService As IViewManagerService, ByVal objIViewModelUIService As IViewModelUIService)
    MyBase.New(objIViewModelUIService)
End Sub



Public Sub GetShipments()
    Dim InitialResults = From shipment In db.Shipment.Include("Packages") _
                         Select shipment
    Me.Shipments = New ShipmentsCollection(InitialResults, db)
End Sub

So I declared a new Thread, assigned it the GetShipments method and instanced it in the default constructor. Ocean freaks out at this, so there must be a better way of doing it.

I have not had the chance to figure out the usage of the SQL ORM thing in Ocean so I am using Entity Framework (perhaps one of these days i will look at NHibernate or something too).

Any information would be greatly appreciated. I have looked at a number of articles and they all have examples of simple uses. Some have mentioned the Dispatcher, but none really go very far into how it is used. Anyone know any good tutorials?

Cory

© Stack Overflow or respective owner

Related posts about wpf

Related posts about vb.net