數據加載等待頁面的VB.NET代碼實現

在網上看到很多的ASP.NET實現,但沒有找到VB.net相關的實現代碼,現根據網上的C#的代碼整理下,改造成爲VB的代碼實現。

其中把實現線程的CLASS放在了頁面類的內部,你可以根據需要自己改動。運行時只需要創建一個相應的頁面,放一一個按鈕和一個LABLE(SERVER端控件),就行


Partial Class _Default
    Inherits System.Web.UI.Page
    Public clkCnt As Integer

    Public task As LengthyTask
    'Private taskCache As System.Web.Caching.Cache

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

        task = CType(Cache.Get("LengthyTask1"), LengthyTask)
        'Cache["LengthyTask"]
        If task Is Nothing Then

            task = New LengthyTask()

            Cache.Insert("LengthyTask1", task)

        End If

        ' The task is already running.

        If (task.Running) Then


            PreparePageWithTaskRunning()

        Else


            PreparePageWithTaskNotRunning()

        End If

    End Sub

    Private Sub PreparePageWithTaskRunning()
        Dim myScript As String

        Button1.Enabled = False

        Label1.Text = "The task is running now.<br>It started at " + task.LastStartTime.ToString() + "<br>" + (DateTime.Now - task.LastStartTime).Seconds.ToString() + " seconds have elapsed"

        ' Register the script to refresh the page every 3 seconds.
        myScript = "<script>window.setTimeout('document.location.replace(document.location.href);',3000);</script>"
        Page.ClientScript.RegisterStartupScript(Me.GetType(), "key", myScript)


    End Sub

 

    Private Sub PreparePageWithTaskNotRunning()


        Label1.Text = "The task is not running."

        If task.FirstRunFinished Then


            Label1.Text += "<br>Last time it started at " + task.LastStartTime.ToString() + "<br> and finished at " + task.LastFinishTime.ToString() + "<br>"

            If (task.LastTaskSuccessful) Then


                Label1.Text += "It succeeded."

 

            Else


                Label1.Text += "It failed."

                If Not (task.ExceptionOccured Is Nothing) Then

                    Label1.Text += "<br>The exception was: " + task.ExceptionOccured.ToString()


                End If
            End If
        End If

    End Sub


    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

        If Not task.Running Then

            task.RunTask()

            PreparePageWithTaskRunning()

        End If


    End Sub

    Class LengthyTask


        ' A flag to indicate whether the task has run at least once.

        Private _firstRunFinished As Boolean = False

        Property FirstRunFinished() As Boolean

            Get
                Return _firstRunFinished
            End Get

            Set(ByVal firstRunfinished As Boolean)
                _firstRunFinished = firstRunfinished
            End Set

        End Property

 

        ' A flag to indicate whether the task is running.

        Private _running As Boolean = False

        Property Running() As Boolean

            Get
                Return _running
            End Get

            Set(ByVal running As Boolean)
                _running = running
            End Set

        End Property

 

        ' A flag to indicate whether the last task succeeded or not.

        Public _lastTaskSuccessful As Boolean = True

        Property LastTaskSuccessful() As Boolean

            Get
                If _lastFinishTime = DateTime.MinValue Then

                    Throw New InvalidOperationException("The task has not finished even once.")
                End If

                Return _lastTaskSuccessful
            End Get

            Set(ByVal value As Boolean)
                _lastTaskSuccessful = value
            End Set

        End Property

 


        ' To store any exception generated during the task.

        Private _exceptionOccured As Exception = Nothing

        Property ExceptionOccured() As Exception

            Get
                Return _exceptionOccured
            End Get

            Set(ByVal value As Exception)
                _exceptionOccured = value
            End Set

        End Property

 

        Private _lastStartTime As Date = DateTime.MinValue

        Property LastStartTime() As Date

            Get
                If _lastStartTime = DateTime.MinValue Then

                    Throw New InvalidOperationException("The task has not started even once.")
                End If
                Return _lastStartTime

            End Get

            Set(ByVal value As Date)
                _lastStartTime = value
            End Set

        End Property

 

        Private _lastFinishTime As Date = DateTime.MinValue

        Property LastFinishTime() As Date

            Get
                If _lastFinishTime = DateTime.MinValue Then

                    Throw New InvalidOperationException("The task has not finished even once.")
                End If
                Return _lastFinishTime
            End Get

            Set(ByVal value As Date)
                _lastFinishTime = value
            End Set

        End Property

 

        ' Start the task

        Public Sub RunTask()


            ' Only one thread is allowed to enter here.

            SyncLock Me

                If (Not _running) Then


                    _running = True

                    _lastStartTime = DateTime.Now
                    Dim t As Threading.Thread = New Threading.Thread(New Threading.ThreadStart(AddressOf Me.ThreadWork))
                    t.Start()

                Else

                    Throw New InvalidOperationException("The task is already running!")


                End If
            End SyncLock

        End Sub

 

        Public Sub ThreadWork()


            Try


                ' Suppose that we need to run the DTS package here.

                ' Replace the following line with your code.

                Threading.Thread.Sleep(20000)

                ' Setting successful flag.

                _lastTaskSuccessful = True

 

            Catch e As Exception


                ' Failed.

                _lastTaskSuccessful = False

                _exceptionOccured = e


            Finally


                _running = False

                _lastFinishTime = DateTime.Now

                If (Not _firstRunFinished) Then _firstRunFinished = True

            End Try

        End Sub

    End Class

End Class
 

發佈了28 篇原創文章 · 獲贊 9 · 訪問量 18萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章