Wednesday, February 29, 2012

mail Asynchronously delegate callback


 
Public Sub sendMail(ByVal subject As String, ByVal message As String)
        Try
            Dim destinations As New ArrayList
             destinations.Add(123@hotmail.com)
            Dim errDesc As String
            'mail is another function to send email
            mail("mail server", "username", "password", "from", subject, message, destinations, Nothing, Nothing, errDesc)
        Catch
        End Try
    End Sub

Public Class SendMailAsyn
    Public Sub sendMailAsyn(ByVal subject As String, ByVal message As String)
        Dim dlgt As New SendMailDelegate(AddressOf sendMail)
        Dim cb As New AsyncCallback(AddressOf SendEmailResponse)
        dlgt.BeginInvoke(subject, message, cb, dlgt)
    End Sub

Public Delegate Sub SendMailDelegate(ByVal subject As String, ByVal message As String)

    'callback method
    Public Sub SendEmailResponse(ByVal ar As IAsyncResult)
        Dim dlgt As SendMailDelegate = CType(ar.AsyncState, SendMailDelegate)
        dlgt = ar.AsyncState
        dlgt.EndInvoke(ar)
    End Sub
End Class

'catch exception and send email
Dim mail As New SendMailAsyn
 mail.sendMailAsyn("Exception", ex.Message)

No comments:

Post a Comment