Passing Additional Information in AsyncCallback

peternm22

New member
Joined
Jun 23, 2007
Messages
3
Programming Experience
Beginner
Hi Everyone!

I'm by no means an expert programmer, so if I get some of the terminology mixed up, please forgive me :)

The application I'm developing is a client application for a web service. The web service sends a tiff file as a fax to a standard fax number. This is working fine, no problems at all.

Now the problem... I would like to be able to log all outgoing faxes in a simple database (most likely a Random Access File), the information I would like to store is the Fax Number, Tiff Filename, and the Transaction ID returned from the web service.

The application uses an AsyncCallback to call SendSingleFaxCompleted() when the web service returns a result. Inside SendSingleFaxCompleted() I can get the Transaction ID, but how can I get the FaxNum and TifLoc from PrepareFax()? SendSingleFaxCompleted() has to have a certain signature to be run from the AsyncCallback, so I can't pass any additional values through it.

I've attached the code below. I'm sure I'm going about this the wrong way, so if someone could point me in the right direction I would really appreciate it.


VB.NET:
   Private Sub PrepareFax(ByVal FaxNum As String, ByVal TifLoc As String)
            Dim XMLDoc As String = GenerateXMLFile(FaxNum, TifLoc, "")

            webMessagingWebService.BeginSendSingleFax(XMLDoc, New AsyncCallback(AddressOf Me.SendSingleFaxCompleted), "SendSingleFax")
    End Sub

    Private Sub SendSingleFaxCompleted(ByVal ar As IAsyncResult)
        If (ar.IsCompleted) Then
            Dim SendSingleFaxResult As MessagingWebService.SendResponse = webMessagingWebService.EndSendSingleFax(ar)

            If Not IsNothing(SendSingleFaxResult) Then
                If Not IsNothing(SendSingleFaxResult.Header) Then
                    If SendSingleFaxResult.Header.ErrorFlag = True Then
                        MsgBox(String.Concat("ReturnMessage = ", SendSingleFaxResult.Header.ReturnMessage))
                    End If
                         Dim TransID as String = SendSingleFaxResult.TransactionID
                         'now that I have the transaction ID here, I want to write
                         'a database entry with the FaxNum and TifLoc, but I have
                         'no way of access them
                End If

            End If
        Else
            MsgBox("The call to SendSingleFax ended, but apparently never completed.", _
                   MsgBoxStyle.Exclamation And MsgBoxStyle.OKOnly, _
                   "SendSingleFax: An Error Occurred")
        End If
    End Sub

I'm using VB.Net 2003.


Any ideas how I can do this?

Thanks.

-Peter
 
Use the AsyncState 'object' of IAsyncResult to pass something, this corresponds to the 'UserState' parameter when a method is invoked asynchronous.
 
Thanks for the quick reply.

It looks like AsyncState 'object' is already set to "SendSingleFax", which I think is needed by the web service. Or am I wrong?

Thanks.

-Peter
 
I had a look at how .Net 1.1 generates webservice proxy classes and it is a little different method naming than .Net 2.0 but they work the same. You are correct AsyncState object now is string "SendSingleFax", but you are wrong about the service needing this, the service doesn't know anything about this value, it is an exclusive local threading value that you can use for what you want. So your ar.AsyncState in callback should currently read "SendSingleFax".
 
Back
Top