实现数据加载等待页面的VB.NET代码的方法

实现数据加载等待页面的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