Xcoordination in VB

Jul 1, 2010 at 8:35 PM

I'm just wondering if one of you smart C# guys can hold your nose for a moment and help me figure out what I'm doing wrong here .  I tried converting the RequestResponse sample but without any luck.  It blows by the 

w.Post(New Add() With {.Num1 = 1, .Num2 = 2, .ResponsePort = responsePort}) without entering the Process Sub in the worker class.   BTW can you show me how to do the cool inline function thing in VB like you did on the Space.Recieve line?   
Thanks.

Jon

Namespace RequestResponse

    Module Program

        Sub Main()

            Console.WriteLine("Xco Application Space - Request/Response Demo - [Enter] to finish")

            Try
                Using space = New XcoAppSpace()

                    Dim w = space.RunWorker(Of PMathWorker, MyMathWorker)()

                    Dim responsePort = New Port(Of Integer)()

                    space.Receive(responsePort, New Handler(Of Integer)(AddressOf ResponseRecieved))

                    w.Post(New Add() With {.Num1 = 1, .Num2 = 2, .ResponsePort = responsePort})

                    w.Post(New Add() With {.Num1 = 23, .Num2 = 96, .ResponsePort = responsePort})

                    w.Post(New Add() With {.Num1 = 7437, .Num2 = 9345, .ResponsePort = responsePort})

                    Console.ReadLine()

                End Using

            Catch ex As Exception
                Console.WriteLine(ex.Message)
                Console.ReadLine()
            End Try
        End Sub

        Private Sub ResponseRecieved(ByVal Result As Integer)
            Console.WriteLine(Result)
        End Sub

    End Module

End Namespace

Namespace RequestResponse

    Class PMathWorker

        Inherits Port(Of Add)

    End Class

    Class Add

        Public Property Num1() As Integer

            Get

                Return m_Num1

            End Get

            Set(ByVal value As Integer)

                m_Num1 = Value

            End Set

        End Property

        Private m_Num1 As Integer

        Public Property Num2() As Integer

            Get

                Return m_Num2

            End Get

            Set(ByVal value As Integer)

                m_Num2 = Value

            End Set

        End Property

        Private m_Num2 As Integer

        Public Property ResponsePort() As Port(Of Integer)

            Get

                Return m_ResponsePort

            End Get

            Set(ByVal value As Port(Of Integer))

                m_ResponsePort = Value

            End Set

        End Property

        Private m_ResponsePort As Port(Of Integer)

        'pass a port to the worker to receive a result to the request

    End Class

    'implementation

    Class MyMathWorker

        Inherits PMathWorker

        <XcoConcurrent()> _
        Private Sub Process(ByVal add As Add)

            Console.WriteLine("Processing addition: " + add.Num1 + " + " + add.Num2)

            Dim result As Integer = add.Num1 + add.Num2

            add.ResponsePort.Post(result)

        End Sub

    End Class

End Namespace



Coordinator
Jul 2, 2010 at 6:15 AM

The problem is tiny: you just forgot to do a string conversion in the worker.

The line to log the numbers to add needs to read:

Console.WriteLine("Processing addition: " + add.Num1.ToString() + " + " + add.Num2.ToString())

That´s it.

But why did you not see the exception that was thrown? Because you had not set up a causality. Here´s what you should do at least if the results are not as expected ;-)

Dim exPort As New Port(Of Exception)
space.Receive(exPort, Sub(ex As Exception) Console.WriteLine("*** {0}", ex.Message))
Dim c As New Causality("catch errors", exPort)
Dispatcher.AddCausality(c)

Insert this code before your first call to w.Post().

As for the cool inline functions: see the Sub() in the space.Receive() call. That´s a lambda procedure in VB of VS2010. In VS2008 you can´t do that yet.

-Ralf

 

Jul 2, 2010 at 12:42 PM

Thanks Ralf,

 

Works great!  Thanks for the help.