Results 1 to 10 of 10

Thread: [2008] Trying to call a method asyncronously

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2007
    Posts
    180

    [2008] Trying to call a method asyncronously

    im making a transport agent for exchange and am getting hung up on how to call a method asyncronously. Im doing this based on an msdn article but like usual they didnt provide the part of the code i need to figure out.

    the article is in C# but i converted it to vb in my project.
    http://msdn.microsoft.com/en-us/library/cc720860.aspx

    I know I need to setup a delegate for the method and then invoke that delegate, but im not 100% on how to implement this.

    Here is the code. AsyncDBCallback is the method i am trying to call. Any help would be appreciated.

    Code:
    Imports Microsoft.Exchange.Data.Transport.Routing
    Imports Microsoft.Exchange.Data.Transport
    
    Namespace TestTransportAgent
        NotInheritable Class TestAgentFactory
            Inherits RoutingAgentFactory
    
            Public Overrides Function CreateAgent(ByVal server As Microsoft.Exchange.Data.Transport.SmtpServer) As Microsoft.Exchange.Data.Transport.Routing.RoutingAgent
                Return New MyTestAgent
            End Function
        End Class
    
        Public Class MyTestAgent
            Inherits RoutingAgent
    
            Private Sub SubmittedMessageHandler(ByVal source As SubmittedMessageEventSource, ByVal e As QueuedMessageEventArgs) Handles Me.OnSubmittedMessage
                Dim asyncState As New AsyncState(Of SubmittedMessageEventSource, QueuedMessageEventArgs)(source, e, Me.GetAgentAsyncContext())
                Dim caller As New DelCallBackFunction(AddressOf AsyncDBCallback)
                'not sure how to implement it from thsi point
    Dim result As IAsyncResult = caller.BeginInvoke(
            End Sub
    
            Delegate Sub DelCallBackFunction(ByVal ar As IAsyncResult)
    
            Private Sub AsyncDBCallback(ByVal ar As IAsyncResult)
                Dim asyncState As AsyncState(Of SubmittedMessageEventSource, QueuedMessageEventArgs) = DirectCast(ar.AsyncState, AsyncState(Of SubmittedMessageEventSource, QueuedMessageEventArgs))
    
                'Check message and send to web service
                If asyncState.Args.MailItem.Recipients.Contains("[email protected]") And asyncState.Args.MailItem.Message.Subject.Contains("chicken") Then
                    Dim testt As webservice.Service = New webservice.Service
                    Dim msg As String = asyncState.Args.MailItem.Message.Body.ToString
    
                    Try
                        testt.HelloWorld(msg)
                    Catch ex As Exception
                        Throw New Exception(Err.Description & " " & ex.Message)
                    End Try
                ElseIf asyncState.Args.MailItem.Recipients.Contains("[email protected]") Then
                    Dim testt As webservice.Service = New webservice.Service
                    testt.HelloWorld("secondstatement")
                End If
    
                asyncState.Complete()
            End Sub
    
            Private Class AsyncState(Of SourceType, ArgsType)
                Private m_source As SourceType
                Private m_args As ArgsType
                Private asyncContext As AgentAsyncContext
    
                Public Sub New(ByVal source As SourceType, ByVal args As ArgsType, ByVal asyncContext As AgentAsyncContext)
                    Me.m_source = source
                    Me.m_args = args
                    Me.asyncContext = asyncContext
                End Sub
    
                Public ReadOnly Property Source() As SourceType
                    Get
                        Return Me.m_source
                    End Get
                End Property
    
                Public ReadOnly Property Args() As ArgsType
                    Get
                        Return Me.m_args
                    End Get
                End Property
    
                Public Sub Complete()
                    Me.asyncContext.Complete()
    
                    Me.m_source = Nothing
                    Me.m_args = Nothing
                    Me.asyncContext = Nothing
                End Sub
            End Class
        End Class
    End Namespace

  2. #2
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [2008] Trying to call a method asyncronously

    Well to setup a delegate you simply do this (in this example we will pretend the method you are invoking just takes one string as an argument):
    vb Code:
    1. Delegate Sub MyDelegate(Byval SomeString As String)

    And to invoke it you just do this:
    vb.net Code:
    1. Private Sub SomeExampleSub
    2.     Invoke(New SomeDelegate(AddressOf TheSubIWantToRun), TheStringIWantToPassIn)
    3. End Sub
    Where "TheSubIWantToRun" would be the name of the sub that you actually want to pass this string to and the sub that will do whatever it is you are trying to 'invoke'

    Hope that helps
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Nov 2007
    Posts
    180

    Re: [2008] Trying to call a method asyncronously

    Im getting "overload resolution failed because no 'Invoke' is accessible" on the invoke part of the code.

    Code:
    Invoke(New DelCallBackFunction(AddressOf AsyncDBCallback), asyncState)
            End Sub
    
    Delegate Sub DelCallBackFunction(ByVal ar As IAsyncResult)

  4. #4
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [2008] Trying to call a method asyncronously

    Can you post a bit more the code around the bit you just posted so we can see exactly where you are using it?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Nov 2007
    Posts
    180

    Re: [2008] Trying to call a method asyncronously

    Hope this helps. Also in the msdn link they you might be able to get a gist of whats going on by reading the comments they have, to bad its in c# and they didnt provide a vb.net version

    http://msdn.microsoft.com/en-us/library/cc720860.aspx

    Code:
    Private Sub SubmittedMessageHandler(ByVal source As SubmittedMessageEventSource, ByVal e As QueuedMessageEventArgs) Handles Me.OnSubmittedMessage
                Dim asyncState As New AsyncState(Of SubmittedMessageEventSource, QueuedMessageEventArgs)(source, e, Me.GetAgentAsyncContext())
                Invoke(New DelCallBackFunction(AddressOf AsyncDBCallback), asyncState)
    End Sub
    
    Delegate Sub DelCallBackFunction(ByVal ar As IAsyncResult)
    
    Private Sub AsyncDBCallback(ByVal ar As IAsyncResult)
    'code that does stuff
    End Sub
    
    Private Class AsyncState(Of SourceType, ArgsType)
                Private m_source As SourceType
                Private m_args As ArgsType
                Private asyncContext As AgentAsyncContext
                'public properties for each of the above variable
    End Class
    Last edited by Metallicaman; Feb 4th, 2009 at 02:30 PM.

  6. #6
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [2008] Trying to call a method asyncronously

    Well that code you just posted is different to the section you posted in your last post lol

    The invoke line in this code is not passing in the IAsyncResult object that is required by the delegate you are trying to invoke.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Nov 2007
    Posts
    180

    Re: [2008] Trying to call a method asyncronously

    whoops my bad.

    updated the code, I was messing around with guess I didnt change all the code back.

    edit - also in the MSDN article this comment is what im having trouble implementing. That comment is place in the SubmittedMessageHandler right where that invoke code I put is.

    // Call an asynchronous I/O method.
    // Pass in this.AsyncIOCallback as the callback and
    // asyncState as the state argument.
    Last edited by Metallicaman; Feb 4th, 2009 at 02:35 PM.

  8. #8
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2008] Trying to call a method asyncronously

    The class you're inheriting (RoutingAgent) doesnt seem to expose an Invoke method, so you wont be able to call it like that. However delegates have Invoke methods:
    Code:
    Dim caller As New DelCallBackFunction(AddressOf AsyncDBCallback)
    caller.Invoke(asyncState)
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Nov 2007
    Posts
    180

    Re: [2008] Trying to call a method asyncronously

    Ok lemme give that a try

  10. #10
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [2008] Trying to call a method asyncronously

    Ohh I didnt realise Invoke was a method, I thought it was just a command/keyword (like Dim and New etc)... I guess just because it is normally always available because I'm used to always attempting to use it from within a form class. Although the fact its not shown in the same way in intellisense should have given me a clue... but what can I say. Learn something everyday
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width