开发者

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

开发者 https://www.devze.com 2022-12-24 20:32 出处:网络
I have started messing around with the MVVP pattern, and I am having some problems with UI responsiveness versus data processing.

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 def开发者_如何学Cault 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).

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.


The way I do it is, using a backgroundworker. Then I use MVVM Light to send a message to the main ui to update progress.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号