Calling synch within a web service

VBAHole22

Contributor
Joined
Oct 21, 2003
Messages
432
Location
VA
Calling asynch within a web service

Previously I had a project split into an asp.net page and a web service. I have two 'worker' web methods that can run side by side (asynch) and I found that I got a speed advantage when I did that. The way I called it was:

PHP:
Dim obj As New TransService.TransService
        obj.Timeout = -1
Dim ConvertResult As IAsyncResult
    Try
        OracleConn.Open()
        ConvertResult = obj.BeginConvert(LOCAL_Path, Zone, Unit, FileType, Nothing, Nothing)
    Catch ex As Exception
    Finally
          OracleConn.Close()
    End Try

And I called the other just like it. Then I would wait for the response before moving on:

PHP:
Try
            Dim asyncResult As String
            asyncResult = obj.EndConvert(ConvertAnnoResult)
            If asyncResult <> "Success" Then
                Me.txtError.Text = "Error in the Convert procedure :: " & asyncResult
            End If
Catch ex As Exception
End Try


But now I have moved this calling code into the web service itself. I was wondering if there is a way to call these two methods from within the web service asynchronously?
Basically what I did was encapsulate my web service so that it only exposes a single web method and then that web method calls other private functions within the web service.
Do I need to use threads?
 
Last edited:
I'm making some headway taking Derek's tutorial on threading and trying to incorporate that into my web service. But all this business with Delegates and AddressOf can get you twisted around in knots pulling your hair out.
I know I need to call 2 methods on 2 different threads and I need to know the return value (a String). I need to wait for both to finish before moving ahead and I need to assess the resulting Strings that come back.

/--------\
............. ====


Sorta like that. I hope to gain some speed by having these two procs run side by side.
Trouble is I can't figure out how to modify the sample to meet my needs. Each of my procedures accepts 4 strings and returns 1.

Here is what I have so far and it "ain't compilin" as they say.

PHP:
Try
            OracleConn.Open()
            Dim ta As New ThreadArgumentsAnno
            ta.LOCAL = LOCAL_Path
            ta.ZONE = Zone
            ta.Unit = Unit
            ta.FileType = FileType
            ta.ThreadDelegate = AddressOf TertiaryThread
            AddHandler ta.ThreadReturned, AddressOf TertiaryThreadDone
            Dim AnnoThread As New Thread(AddressOf ta.Start)
            AnnoThread.Start()
        Catch ex As Exception
            blSuccess = False
        Finally
            OracleConn.Close()
        End Try


 Delegate Function TertiaryThreadDelegate(ByVal Message As String) As Boolean
    Delegate Sub ChangeLabelDelegate(ByVal Text As String)
    Private Class ThreadArgumentsAnno
        Public LOCAL, ZONE, Unit, FileType As String
        Public ThreadDelegate As TertiaryThreadDelegate
        Public Event ThreadReturned(ByVal sender As Object, ByVal e As ThreadReturnArgs)
        Public Sub Start()
            Dim b As Boolean = ThreadDelegate(Message)
            Dim a As New ThreadReturnArgs(b)
            RaiseEvent ThreadReturned(Me, a)
        End Sub
    End Class

    Private Class ThreadReturnArgs
        Inherits EventArgs
        Dim m_arg As String
        Sub New(ByVal arg As String)
            m_arg = arg
        End Sub
        Public ReadOnly Property ThreadReturn() As String
            Get
                Return m_arg
            End Get
        End Property
    End Class
    Private Function TertiaryThread(ByVal Message As String) As Boolean
        'Me.Label1.Invoke(New ChangeLabelDelegate(AddressOf ChangeLabel), New Object() {"Thread has finished executing"})
        Return True
    End Function
    Private Sub TertiaryThreadDone(ByVal sender As Object, ByVal e As ThreadReturnArgs)
        'MessageBox.Show("Thread returned a value of """ & e.ThreadReturn.ToString() & """", Application.ProductName)
    End Sub
    Private Sub ChangeLabel(ByVal Text As String)
        'Me.Label1.Text = Text
    End Sub

I commented out the messagebox because I am in a web service. I guess I don't need the changelabel, or is that where I call my method?

Previously I was calling it like so:

ConvertResult = Convert(LOCAL_Path, Zone, Unit, FileType)
 
I got it to work!!!! Well, all I really did was modify Derek's code to include more input parameters and a string output param and add another copy of it for a different thread.

I only have one more issue:
In my calling code I woudl like to know the value of the return parameter from the function? I can get at it during the TertiaryThreadDone Sub as e.ThreadReturn. However how can i get at it in my main procedure code?
I can't just reference it as ThreadReturn because that is a property of a class and I don't have a ThreadreturnArgs object in my calling code. If I instantiated one it wouldn't be the same one right? I am trying my hardest to avoid using a global variable because I know those are poison.

And ideas?

PHP:
 Private Class ThreadReturnArgs
        Inherits EventArgs
        Dim m_arg As Boolean
        Sub New(ByVal arg As Boolean)
            m_arg = arg
        End Sub
        Public ReadOnly Property ThreadReturn() As Boolean
            Get
                Return m_arg
            End Get
        End Property
    End Class
 
Back
Top