Author:水如烟
Imports
System.ComponentModel
Imports System.Threading
Namespace LzmTW.uSystem.uWindows.uForms
Friend Class ProgressForm
Private gAction As Threading.ThreadStart
Private gResult As ProgressFormActionResult
Private gControl As Object = Nothing
Private gActionInstance As Object = Nothing
Private Sub OnProgressFormStyleChanged( ByVal sender As Object , ByVal style As ProgressFormStyle)
Select Case style
Case ProgressFormStyle.OnlyMessage
Me .ButtonCancel.Visible = False
Me .ProgressBar.Visible = False
Case ProgressFormStyle.ProgressWithCancel
Me .ButtonCancel.Visible = True
Me .ProgressBar.Visible = True
Case ProgressFormStyle.ProgressWithNoCancel
Me .ButtonCancel.Visible = False
Me .ProgressBar.Visible = True
End Select
End Sub
Public Overloads Sub Show( ByVal style As ProgressFormStyle)
If style = ProgressFormStyle.ProgressWithCancel Then style = ProgressFormStyle.ProgressWithNoCancel
Me .OnProgressFormStyleChanged( Nothing , style)
MyBase .Show()
End Sub
Public Overloads Sub Show( ByVal action As Threading.ThreadStart, ByVal style As ProgressFormStyle)
gAction = action
Me .BackgroundWorker.WorkerSupportsCancellation = (style = ProgressFormStyle.ProgressWithCancel)
Me .OnProgressFormStyleChanged( Nothing , style)
MyBase .Show()
End Sub
Public Overloads Sub Show( ByVal action As Threading.ThreadStart, ByVal ctr As Object , ByVal style As ProgressFormStyle)
' action不可控
style = ProgressFormStyle.OnlyMessage
Me .MessageLabel.Text = " 正在进行中... "
gAction = action
gControl = ctr
Me .BackgroundWorker.WorkerSupportsCancellation = (style = ProgressFormStyle.ProgressWithCancel)
Me .OnProgressFormStyleChanged( Nothing , style)
MyBase .Show()
End Sub
Public Overloads Sub Show( ByVal action As Threading.ThreadStart, ByVal ctr As Object , ByVal actionInstance As Object , ByVal style As ProgressFormStyle)
' action不可控
style = ProgressFormStyle.OnlyMessage
Me .MessageLabel.Text = " 正在进行中... "
gAction = action
gControl = ctr
gActionInstance = actionInstance
Me .BackgroundWorker.WorkerSupportsCancellation = (style = ProgressFormStyle.ProgressWithCancel)
Me .OnProgressFormStyleChanged( Nothing , style)
MyBase .Show()
End Sub
Private Sub BackgroundWorker_DoWork( ByVal sender As Object , ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker.DoWork
Me .ActionInvoke( CType (sender, BackgroundWorker), e)
End Sub
Private Sub BackgroundWorker_RunWorkerCompleted( ByVal sender As Object , ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker.RunWorkerCompleted
If e.Cancelled Then
gResult = ProgressFormActionResult.Cancel
Else
gResult = ProgressFormActionResult.Finish
End If
Me .Close()
End Sub
Private Sub ButtonCancel_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonCancel.Click
Me .BackgroundWorker.CancelAsync()
End Sub
Private Sub ProgressForm_Shown( ByVal sender As Object , ByVal e As System.EventArgs) Handles Me .Shown
If gAction = Nothing Then Exit Sub
Me .BackgroundWorker.RunWorkerAsync()
End Sub
Private Sub ActionInvoke( ByVal worker As BackgroundWorker, ByVal e As DoWorkEventArgs)
Dim mThread As Threading.Thread = Nothing
If gActionInstance IsNot Nothing Then
mThread = New Thread( AddressOf WaitingOtherInvokeAction)
ElseIf gControl IsNot Nothing Then
mThread = New Thread( AddressOf WaitingInvokeAction)
Else
mThread = New Thread( AddressOf SimpleInvokeAction)
End If
mThread.Name = " ProgressForm " & Now.Ticks
mThread.Start()
While mThread.IsAlive
If worker.CancellationPending Then
e.Cancel = True
mThread.Abort()
End If
End While
End Sub
Private Sub SimpleInvokeAction( ByVal state As Object )
gAction.Invoke()
End Sub
Private Sub WaitingInvokeAction( ByVal state As Object )
uThreading.CrossThread.UpdateControlByCurrentClassThreadAction(gControl, gAction)
End Sub
Private Sub WaitingOtherInvokeAction( ByVal state As Object )
uThreading.CrossThread.UpdateControlByOtherClassThreadAction(gControl, gActionInstance, gAction)
End Sub
' Private asyncOpsAreDone As New AutoResetEvent(False)
' Private Sub ActionInvoke(ByVal worker As BackgroundWorker, ByVal e As DoWorkEventArgs)
' If gActionInstance IsNot Nothing Then
' ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf WaitingOtherInvokeAction), asyncOpsAreDone)
' ElseIf gControl IsNot Nothing Then
' ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf WaitingInvokeAction), asyncOpsAreDone)
' Else
' ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf SimpleInvokeAction), asyncOpsAreDone)
' End If
' asyncOpsAreDone.WaitOne()
' End Sub
' Private Sub SimpleInvokeAction(ByVal state As Object)
' gAction.Invoke()
' CType(state, AutoResetEvent).Set()
' End Sub
' Private Sub WaitingInvokeAction(ByVal state As Object)
' uThreading.CrossThread.UpdateControlByCurrentClassThreadAction(gControl, gAction)
' CType(state, AutoResetEvent).Set()
' End Sub
' Private Sub WaitingOtherInvokeAction(ByVal state As Object)
' uThreading.CrossThread.UpdateControlByOtherClassThreadAction(gControl, gActionInstance, gAction)
' CType(state, AutoResetEvent).Set()
' End Sub
End Class
End Namespace
Imports System.Threading
Namespace LzmTW.uSystem.uWindows.uForms
Friend Class ProgressForm
Private gAction As Threading.ThreadStart
Private gResult As ProgressFormActionResult
Private gControl As Object = Nothing
Private gActionInstance As Object = Nothing
Private Sub OnProgressFormStyleChanged( ByVal sender As Object , ByVal style As ProgressFormStyle)
Select Case style
Case ProgressFormStyle.OnlyMessage
Me .ButtonCancel.Visible = False
Me .ProgressBar.Visible = False
Case ProgressFormStyle.ProgressWithCancel
Me .ButtonCancel.Visible = True
Me .ProgressBar.Visible = True
Case ProgressFormStyle.ProgressWithNoCancel
Me .ButtonCancel.Visible = False
Me .ProgressBar.Visible = True
End Select
End Sub
Public Overloads Sub Show( ByVal style As ProgressFormStyle)
If style = ProgressFormStyle.ProgressWithCancel Then style = ProgressFormStyle.ProgressWithNoCancel
Me .OnProgressFormStyleChanged( Nothing , style)
MyBase .Show()
End Sub
Public Overloads Sub Show( ByVal action As Threading.ThreadStart, ByVal style As ProgressFormStyle)
gAction = action
Me .BackgroundWorker.WorkerSupportsCancellation = (style = ProgressFormStyle.ProgressWithCancel)
Me .OnProgressFormStyleChanged( Nothing , style)
MyBase .Show()
End Sub
Public Overloads Sub Show( ByVal action As Threading.ThreadStart, ByVal ctr As Object , ByVal style As ProgressFormStyle)
' action不可控
style = ProgressFormStyle.OnlyMessage
Me .MessageLabel.Text = " 正在进行中... "
gAction = action
gControl = ctr
Me .BackgroundWorker.WorkerSupportsCancellation = (style = ProgressFormStyle.ProgressWithCancel)
Me .OnProgressFormStyleChanged( Nothing , style)
MyBase .Show()
End Sub
Public Overloads Sub Show( ByVal action As Threading.ThreadStart, ByVal ctr As Object , ByVal actionInstance As Object , ByVal style As ProgressFormStyle)
' action不可控
style = ProgressFormStyle.OnlyMessage
Me .MessageLabel.Text = " 正在进行中... "
gAction = action
gControl = ctr
gActionInstance = actionInstance
Me .BackgroundWorker.WorkerSupportsCancellation = (style = ProgressFormStyle.ProgressWithCancel)
Me .OnProgressFormStyleChanged( Nothing , style)
MyBase .Show()
End Sub
Private Sub BackgroundWorker_DoWork( ByVal sender As Object , ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker.DoWork
Me .ActionInvoke( CType (sender, BackgroundWorker), e)
End Sub
Private Sub BackgroundWorker_RunWorkerCompleted( ByVal sender As Object , ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker.RunWorkerCompleted
If e.Cancelled Then
gResult = ProgressFormActionResult.Cancel
Else
gResult = ProgressFormActionResult.Finish
End If
Me .Close()
End Sub
Private Sub ButtonCancel_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonCancel.Click
Me .BackgroundWorker.CancelAsync()
End Sub
Private Sub ProgressForm_Shown( ByVal sender As Object , ByVal e As System.EventArgs) Handles Me .Shown
If gAction = Nothing Then Exit Sub
Me .BackgroundWorker.RunWorkerAsync()
End Sub
Private Sub ActionInvoke( ByVal worker As BackgroundWorker, ByVal e As DoWorkEventArgs)
Dim mThread As Threading.Thread = Nothing
If gActionInstance IsNot Nothing Then
mThread = New Thread( AddressOf WaitingOtherInvokeAction)
ElseIf gControl IsNot Nothing Then
mThread = New Thread( AddressOf WaitingInvokeAction)
Else
mThread = New Thread( AddressOf SimpleInvokeAction)
End If
mThread.Name = " ProgressForm " & Now.Ticks
mThread.Start()
While mThread.IsAlive
If worker.CancellationPending Then
e.Cancel = True
mThread.Abort()
End If
End While
End Sub
Private Sub SimpleInvokeAction( ByVal state As Object )
gAction.Invoke()
End Sub
Private Sub WaitingInvokeAction( ByVal state As Object )
uThreading.CrossThread.UpdateControlByCurrentClassThreadAction(gControl, gAction)
End Sub
Private Sub WaitingOtherInvokeAction( ByVal state As Object )
uThreading.CrossThread.UpdateControlByOtherClassThreadAction(gControl, gActionInstance, gAction)
End Sub
' Private asyncOpsAreDone As New AutoResetEvent(False)
' Private Sub ActionInvoke(ByVal worker As BackgroundWorker, ByVal e As DoWorkEventArgs)
' If gActionInstance IsNot Nothing Then
' ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf WaitingOtherInvokeAction), asyncOpsAreDone)
' ElseIf gControl IsNot Nothing Then
' ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf WaitingInvokeAction), asyncOpsAreDone)
' Else
' ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf SimpleInvokeAction), asyncOpsAreDone)
' End If
' asyncOpsAreDone.WaitOne()
' End Sub
' Private Sub SimpleInvokeAction(ByVal state As Object)
' gAction.Invoke()
' CType(state, AutoResetEvent).Set()
' End Sub
' Private Sub WaitingInvokeAction(ByVal state As Object)
' uThreading.CrossThread.UpdateControlByCurrentClassThreadAction(gControl, gAction)
' CType(state, AutoResetEvent).Set()
' End Sub
' Private Sub WaitingOtherInvokeAction(ByVal state As Object)
' uThreading.CrossThread.UpdateControlByOtherClassThreadAction(gControl, gActionInstance, gAction)
' CType(state, AutoResetEvent).Set()
' End Sub
End Class
End Namespace
Namespace
LzmTW.uSystem.uWindows.uForms
< Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated() > _
Partial Class ProgressForm
Inherits System.Windows.Forms.Form
' Form 重写 Dispose,以清理组件列表。
< System.Diagnostics.DebuggerNonUserCode() > _
Protected Overrides Sub Dispose( ByVal disposing As Boolean )
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
MyBase .Dispose(disposing)
End Sub
' Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
' 注意: 以下过程是 Windows 窗体设计器所必需的
' 可以使用 Windows 窗体设计器修改它。
' 不要使用代码编辑器修改它。
< System.Diagnostics.DebuggerStepThrough() > _
Private Sub InitializeComponent()
Me .TableLayoutPanel1 = New System.Windows.Forms.TableLayoutPanel
Me .ButtonCancel = New System.Windows.Forms.Button
Me .ProgressBar = New System.Windows.Forms.ProgressBar
Me .MessageLabel = New System.Windows.Forms.Label
Me .BackgroundWorker = New System.ComponentModel.BackgroundWorker
Me .TableLayoutPanel1.SuspendLayout()
Me .SuspendLayout()
'
' TableLayoutPanel1
'
Me .TableLayoutPanel1.ColumnCount = 2
Me .TableLayoutPanel1.ColumnStyles.Add( New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100.0 !))
Me .TableLayoutPanel1.ColumnStyles.Add( New System.Windows.Forms.ColumnStyle)
Me .TableLayoutPanel1.Controls.Add( Me .ButtonCancel, 1 , 0 )
Me .TableLayoutPanel1.Controls.Add( Me .ProgressBar, 0 , 1 )
Me .TableLayoutPanel1.Controls.Add( Me .MessageLabel, 0 , 0 )
Me .TableLayoutPanel1.Location = New System.Drawing.Point( 0 , 3 )
Me .TableLayoutPanel1.Name = " TableLayoutPanel1 "
Me .TableLayoutPanel1.RowCount = 2
Me .TableLayoutPanel1.RowStyles.Add( New System.Windows.Forms.RowStyle)
Me .TableLayoutPanel1.RowStyles.Add( New System.Windows.Forms.RowStyle)
Me .TableLayoutPanel1.Size = New System.Drawing.Size( 572 , 95 )
Me .TableLayoutPanel1.TabIndex = 0
'
' ButtonCancel
'
Me .ButtonCancel.Location = New System.Drawing.Point( 478 , 3 )
Me .ButtonCancel.Name = " ButtonCancel "
Me .ButtonCancel.Size = New System.Drawing.Size( 91 , 33 )
Me .ButtonCancel.TabIndex = 1
Me .ButtonCancel.Text = " 取消 "
Me .ButtonCancel.UseVisualStyleBackColor = True
'
' ProgressBar
'
Me .TableLayoutPanel1.SetColumnSpan( Me .ProgressBar, 2 )
Me .ProgressBar.Location = New System.Drawing.Point( 3 , 71 )
Me .ProgressBar.Name = " ProgressBar "
Me .ProgressBar.Size = New System.Drawing.Size( 566 , 17 )
Me .ProgressBar.Style = System.Windows.Forms.ProgressBarStyle.Marquee
Me .ProgressBar.TabIndex = 2
'
' MessageLabel
'
Me .MessageLabel.Location = New System.Drawing.Point( 3 , 0 )
Me .MessageLabel.Name = " MessageLabel "
Me .MessageLabel.Size = New System.Drawing.Size( 469 , 68 )
Me .MessageLabel.TabIndex = 0
'
' BackgroundWorker
'
'
' ProgressForm
'
Me .AutoScaleDimensions = New System.Drawing.SizeF( 6.0 !, 12.0 !)
Me .AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me .ClientSize = New System.Drawing.Size( 577 , 100 )
Me .ControlBox = False
Me .Controls.Add( Me .TableLayoutPanel1)
Me .Name = " ProgressForm "
Me .ShowInTaskbar = False
Me .StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me .Text = " 请稍候 "
Me .TopMost = True
Me .TableLayoutPanel1.ResumeLayout( False )
Me .ResumeLayout( False )
End Sub
Friend WithEvents TableLayoutPanel1 As System.Windows.Forms.TableLayoutPanel
Friend WithEvents MessageLabel As System.Windows.Forms.Label
Friend WithEvents ButtonCancel As System.Windows.Forms.Button
Friend WithEvents ProgressBar As System.Windows.Forms.ProgressBar
Friend WithEvents BackgroundWorker As System.ComponentModel.BackgroundWorker
End Class
End Namespace
< Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated() > _
Partial Class ProgressForm
Inherits System.Windows.Forms.Form
' Form 重写 Dispose,以清理组件列表。
< System.Diagnostics.DebuggerNonUserCode() > _
Protected Overrides Sub Dispose( ByVal disposing As Boolean )
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
MyBase .Dispose(disposing)
End Sub
' Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
' 注意: 以下过程是 Windows 窗体设计器所必需的
' 可以使用 Windows 窗体设计器修改它。
' 不要使用代码编辑器修改它。
< System.Diagnostics.DebuggerStepThrough() > _
Private Sub InitializeComponent()
Me .TableLayoutPanel1 = New System.Windows.Forms.TableLayoutPanel
Me .ButtonCancel = New System.Windows.Forms.Button
Me .ProgressBar = New System.Windows.Forms.ProgressBar
Me .MessageLabel = New System.Windows.Forms.Label
Me .BackgroundWorker = New System.ComponentModel.BackgroundWorker
Me .TableLayoutPanel1.SuspendLayout()
Me .SuspendLayout()
'
' TableLayoutPanel1
'
Me .TableLayoutPanel1.ColumnCount = 2
Me .TableLayoutPanel1.ColumnStyles.Add( New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100.0 !))
Me .TableLayoutPanel1.ColumnStyles.Add( New System.Windows.Forms.ColumnStyle)
Me .TableLayoutPanel1.Controls.Add( Me .ButtonCancel, 1 , 0 )
Me .TableLayoutPanel1.Controls.Add( Me .ProgressBar, 0 , 1 )
Me .TableLayoutPanel1.Controls.Add( Me .MessageLabel, 0 , 0 )
Me .TableLayoutPanel1.Location = New System.Drawing.Point( 0 , 3 )
Me .TableLayoutPanel1.Name = " TableLayoutPanel1 "
Me .TableLayoutPanel1.RowCount = 2
Me .TableLayoutPanel1.RowStyles.Add( New System.Windows.Forms.RowStyle)
Me .TableLayoutPanel1.RowStyles.Add( New System.Windows.Forms.RowStyle)
Me .TableLayoutPanel1.Size = New System.Drawing.Size( 572 , 95 )
Me .TableLayoutPanel1.TabIndex = 0
'
' ButtonCancel
'
Me .ButtonCancel.Location = New System.Drawing.Point( 478 , 3 )
Me .ButtonCancel.Name = " ButtonCancel "
Me .ButtonCancel.Size = New System.Drawing.Size( 91 , 33 )
Me .ButtonCancel.TabIndex = 1
Me .ButtonCancel.Text = " 取消 "
Me .ButtonCancel.UseVisualStyleBackColor = True
'
' ProgressBar
'
Me .TableLayoutPanel1.SetColumnSpan( Me .ProgressBar, 2 )
Me .ProgressBar.Location = New System.Drawing.Point( 3 , 71 )
Me .ProgressBar.Name = " ProgressBar "
Me .ProgressBar.Size = New System.Drawing.Size( 566 , 17 )
Me .ProgressBar.Style = System.Windows.Forms.ProgressBarStyle.Marquee
Me .ProgressBar.TabIndex = 2
'
' MessageLabel
'
Me .MessageLabel.Location = New System.Drawing.Point( 3 , 0 )
Me .MessageLabel.Name = " MessageLabel "
Me .MessageLabel.Size = New System.Drawing.Size( 469 , 68 )
Me .MessageLabel.TabIndex = 0
'
' BackgroundWorker
'
'
' ProgressForm
'
Me .AutoScaleDimensions = New System.Drawing.SizeF( 6.0 !, 12.0 !)
Me .AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me .ClientSize = New System.Drawing.Size( 577 , 100 )
Me .ControlBox = False
Me .Controls.Add( Me .TableLayoutPanel1)
Me .Name = " ProgressForm "
Me .ShowInTaskbar = False
Me .StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me .Text = " 请稍候 "
Me .TopMost = True
Me .TableLayoutPanel1.ResumeLayout( False )
Me .ResumeLayout( False )
End Sub
Friend WithEvents TableLayoutPanel1 As System.Windows.Forms.TableLayoutPanel
Friend WithEvents MessageLabel As System.Windows.Forms.Label
Friend WithEvents ButtonCancel As System.Windows.Forms.Button
Friend WithEvents ProgressBar As System.Windows.Forms.ProgressBar
Friend WithEvents BackgroundWorker As System.ComponentModel.BackgroundWorker
End Class
End Namespace