• Multi Threading on a form

    A friend of mine who writes in VB at his company was telling me about a situtation they're having with long running tasks on their production application. Basically they are polling a database for changes (if it was sql 2005 instead of mysql i'd suggest notification services... not sure if mysql has equivalent). Anyhow, this long running task is locking up the UI so i suggested he use delegates and their built in async pattern to handle this since it's easy constructed and utilized in forms, then he asked how he'd update the UI when the polling completes. In windows forms, there's actually a BeginInvoke method that will let you launch a delegate. I put a tiny and simple example together to get my point across. Here's the form code (basically theres a button and a label on the form

    Imports System.Threading

     

    Public Class Form1

     

        Private Delegate Sub RunAsyncDelegate(ByRef iar As IAsyncResult)

     

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

            Dim d As RunAsyncDelegate = New RunAsyncDelegate(AddressOf RunAsync)

            d.BeginInvoke(Nothing, AddressOf NotifyFinished, Nothing)

     

        End Sub

     

        Private Sub RunAsync(ByRef iar As System.IAsyncResult)

            Thread.Sleep(10000)

        End Sub

     

        Private Delegate Sub NotifyFinishedDelegate(ByVal iar As System.IAsyncResult)

     

        Private Sub NotifyFinished(ByVal iar As System.IAsyncResult)

            If Me.InvokeRequired Then

                Dim d As NotifyFinishedDelegate = New NotifyFinishedDelegate(AddressOf NotifyFinished)

                Me.BeginInvoke(d, iar)

            Else

                Me.Label1.Text = "Work is Finished"

            End If

        End Sub

    End Class

     

    Full story

    Comments (0)

  • Some DM-V-VM thoughts (aka M-V-VM)

    You know I was thinking about this last night a bit, but isn’t a view model just a data model for the view?

     

    Technically it’s just a View Model pattern where the view has it’s own Model?

     

    Really we’re just ‘encapsulating’ (god please don’t let me be one of those guys who uses this word wrong) all of the View’s behavior inside its model like we do for a “Customer” or a “Document”.

     

    Similiarly, like a Customer who might have a child object of “Address” we have a ViewModel who would have a Child object that is “Customer”

    For example we might have this:

     

    Public class ViewModel

    {

    Void OnSetStateCode(string code)

                    {

    Customer.Address.State.TwoDigitCode  = code;

    }

    }

     

     

    So is there a fundamental difference between a view model and a (Data)Model that I’m missing?

    Full story

    Comments (0)