網頁發信Call了就跑範例-非同步作業

  • 2458
  • 0

網頁發信Call了就跑範例-非同步作業

Dotblogs 的標籤: , ,

需求:某個作業完成後,要發信通知相關人員,但是發信成功或失敗,不需告知使用者,也不會造成 Rollbcak。

MailControl.vb

Imports System.Runtime.InteropServices
Imports System.Threading

Public Class MailControl

    Public count As Integer = 0
    Public msg As String = ""

    Public Sub New()
        'nothing
    End Sub

    Public Function SendMail(ByVal msg As String, <Out()> ByRef threadId As Integer) As Boolean
        While (True)
            count += 1
            If count <= 3 Then
                'Console.WriteLine("Send manil fail. count == " + count.ToString())
                Thread.Sleep(1500)
            Else
                'Console.WriteLine("Send mail Success. count == " + count.ToString() + vbCrLf + "msg == " + msg)
                Return True
            End If
        End While
    End Function

End Class

我用兩個頁面來表現這個範例,呼叫發信,三次後才會回傳 True,然後結束時建立檔案來表現作業結束。

FirstPage.vb

Imports System.IO
Imports System.Threading
Imports System.Runtime.InteropServices
Imports System.Runtime.Remoting.Messaging

Partial Public Class FirstPage
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub btnSend_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSend.Click
        CallSendMail()
        Thread.Sleep(1000)
        Response.Redirect("FinishPage.aspx")
    End Sub

    Public Delegate Function AsyncMethodCaller(ByVal msg As String, <Out()> ByRef threadId As Integer) As Boolean

    Public Sub CallSendMail()
        Dim threadId As Integer
        Dim mc As New MailControl()
        Dim caller As New AsyncMethodCaller(AddressOf mc.SendMail)

        Dim result As IAsyncResult = caller.BeginInvoke("This is Leo test string.", threadId, AddressOf CallbackMethod, caller)

    End Sub

    Public Sub CallbackMethod(ByVal ar As IAsyncResult)
        Dim result As AsyncResult = CType(ar, AsyncResult)
        Dim caller As AsyncMethodCaller = CType(result.AsyncDelegate, AsyncMethodCaller)

        Dim threadId As Integer = 0

        Dim returnValue As Boolean = caller.EndInvoke(threadId, ar)

        Dim str As String = Server.MapPath("/") + "testFromAsync" + Now.GetHashCode.ToString + ".txt"
        Dim sw As StreamWriter = File.CreateText(str)
        sw.WriteLine("CallbackMethod finish. Thread Id == " + threadId.ToString())
        sw.Flush()
        sw.Close()
    End Sub
End Class

FinishPage.vb

Imports System.IO

Partial Public Class FinishPage
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub btnList_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnList.Click
        Dim str() As String = Directory.GetFiles(Server.MapPath("/"))
        lblList.Text = ""
        For Each s As String In str
            If s.IndexOf("testFromAsync") > -1 Then
                lblList.Text += s + "<br />"
            End If
        Next

    End Sub
End Class

 

--------
沒什麼特別的~
不過是一些筆記而已