Author:水如烟
Imports
System.Runtime.InteropServices
Namespace LzmTW.uSystem.uThreading
Public Class CrossThread
Private Sub New ()
End Sub
Public Shared Sub SetControlPropertyValue( ByVal ctr As Object , ByVal propertyName As String , ByVal value As Object , ByVal index As Object ())
If TypeOf ctr Is Control Then
Dim mInvoker As Control = CType (ctr, Control)
If mInvoker Is Nothing OrElse mInvoker.IsDisposed Then Exit Sub
If mInvoker.InvokeRequired Then
Dim d As SetControlPropertyValueHandler = New SetControlPropertyValueHandler( AddressOf SetControlPropertyValue)
Try
mInvoker.Invoke(d, New Object () {ctr, propertyName, value, index})
Catch ex As Exception
End Try
Else
uReflection.MemberInfoFunction.SetPropertyValue(ctr, propertyName, value, index)
Application.DoEvents()
End If
ElseIf TypeOf ctr Is ToolStripItem Then
Dim mInvoker As ToolStrip = CType (ctr, ToolStripItem).GetCurrentParent
If mInvoker Is Nothing OrElse mInvoker.IsDisposed Then Exit Sub
If mInvoker.InvokeRequired Then
Dim d As SetControlPropertyValueHandler = New SetControlPropertyValueHandler( AddressOf SetControlPropertyValue)
Try
mInvoker.Invoke(d, New Object () {ctr, propertyName, value, index})
Catch ex As Exception
End Try
Else
uReflection.MemberInfoFunction.SetPropertyValue(ctr, propertyName, value, index)
Application.DoEvents()
End If
End If
End Sub
Public Shared Sub UpdateControlByCurrentClassAction( ByVal ctr As Object , ByVal action As Threading.ThreadStart)
If TypeOf ctr Is Control Then
Dim mInvoker As Control = CType (ctr, Control)
If mInvoker Is Nothing OrElse mInvoker.IsDisposed Then Exit Sub
If mInvoker.InvokeRequired Then
Dim d As UpdateControlByCurrentClassActionHandler = New UpdateControlByCurrentClassActionHandler( AddressOf UpdateControlByCurrentClassAction)
Try
mInvoker.Invoke(d, New Object () {ctr, action})
Catch ex As Exception
End Try
Else
action.Method.Invoke(ctr, Nothing )
End If
ElseIf TypeOf ctr Is ToolStripItem Then
Dim mInvoker As ToolStrip = CType (ctr, ToolStripItem).GetCurrentParent
If mInvoker Is Nothing OrElse mInvoker.IsDisposed Then Exit Sub
If mInvoker.InvokeRequired Then
Dim d As UpdateControlByCurrentClassActionHandler = New UpdateControlByCurrentClassActionHandler( AddressOf UpdateControlByCurrentClassAction)
Try
mInvoker.Invoke(d, New Object () {ctr, action})
Catch ex As Exception
End Try
Else
action.Method.Invoke(ctr, Nothing )
End If
End If
End Sub
Public Shared Sub UpdateControlByOtherClassAction( ByVal ctr As Object , ByVal actionObject As Object , ByVal action As Threading.ThreadStart)
If TypeOf ctr Is Control Then
Dim mInvoker As Control = CType (ctr, Control)
If mInvoker Is Nothing OrElse mInvoker.IsDisposed Then Exit Sub
If mInvoker.InvokeRequired Then
Dim d As UpdateControlByOtherClassActionHandler = New UpdateControlByOtherClassActionHandler( AddressOf UpdateControlByOtherClassAction)
Try
mInvoker.Invoke(d, New Object () {ctr, actionObject, action})
Catch ex As Exception
End Try
Else
action.Method.Invoke(actionObject, Nothing )
End If
ElseIf TypeOf ctr Is ToolStripItem Then
Dim mInvoker As ToolStrip = CType (ctr, ToolStripItem).GetCurrentParent
If mInvoker Is Nothing OrElse mInvoker.IsDisposed Then Exit Sub
If mInvoker.InvokeRequired Then
Dim d As UpdateControlByOtherClassActionHandler = New UpdateControlByOtherClassActionHandler( AddressOf UpdateControlByOtherClassAction)
Try
mInvoker.Invoke(d, New Object () {ctr, actionObject, action})
Catch ex As Exception
End Try
Else
action.Method.Invoke(actionObject, Nothing )
End If
End If
End Sub
Public Shared Sub StartActionByThreading( ByVal threadStart As Threading.ThreadStart, ByVal waiting As Boolean , ByRef abort As Boolean )
Dim t As New Threading.Thread(threadStart)
t.Start()
If waiting Then
While t.IsAlive
Windows.Forms.Application.DoEvents()
If abort Then
Try
t.Abort()
Catch ex As Exception
End Try
End If
End While
End If
abort = True
End Sub
Public Shared Sub StartActionByThreading( ByVal threadStart As Threading.ThreadStart)
StartActionByThreading(threadStart, False , False )
End Sub
Public Shared Sub UpdateControlByCurrentClassThreadAction( ByVal ctr As Object , ByVal action As Threading.ThreadStart, ByRef abort As Boolean )
SyncLock InternalSyncObject
gCtr = ctr
gAction = action
StartActionByThreading( AddressOf ThreadActionByCurrentClass, True , abort)
End SyncLock
End Sub
Public Shared Sub UpdateControlByCurrentClassThreadAction( ByVal ctr As Object , ByVal action As Threading.ThreadStart)
SyncLock InternalSyncObject
gCtr = ctr
gAction = action
StartActionByThreading( AddressOf ThreadActionByCurrentClass)
End SyncLock
End Sub
Private Shared Sub ThreadActionByCurrentClass()
UpdateControlByCurrentClassAction(gCtr, gAction)
End Sub
Public Shared Sub UpdateControlByOtherClassThreadAction( ByVal ctr As Object , ByVal actionObject As Object , ByVal action As Threading.ThreadStart, ByRef abort As Boolean )
SyncLock InternalSyncObject
gCtr = ctr
gAction = action
gActionInstance = actionObject
StartActionByThreading( AddressOf ThreadActionByOtherClass, True , abort)
End SyncLock
End Sub
Public Shared Sub UpdateControlByOtherClassThreadAction( ByVal ctr As Object , ByVal actionObject As Object , ByVal action As Threading.ThreadStart)
SyncLock InternalSyncObject
gCtr = ctr
gAction = action
gActionInstance = actionObject
StartActionByThreading( AddressOf ThreadActionByOtherClass)
End SyncLock
End Sub
Private Shared Sub ThreadActionByOtherClass()
UpdateControlByOtherClassAction(gCtr, gActionInstance, gAction)
End Sub
Private Shared ReadOnly Property InternalSyncObject() As Object
Get
If gInternalSyncObject Is Nothing Then
Dim tmpObj As New Object
System.Threading.Interlocked.CompareExchange(gInternalSyncObject, tmpObj, Nothing )
End If
Return gInternalSyncObject
End Get
End Property
Private Shared gInternalSyncObject As Object
Private Shared gCtr As Object
Private Shared gAction As Threading.ThreadStart
Private Shared gActionInstance As Object
End Class
End Namespace
Namespace LzmTW.uSystem.uThreading
Public Class CrossThread
Private Sub New ()
End Sub
Public Shared Sub SetControlPropertyValue( ByVal ctr As Object , ByVal propertyName As String , ByVal value As Object , ByVal index As Object ())
If TypeOf ctr Is Control Then
Dim mInvoker As Control = CType (ctr, Control)
If mInvoker Is Nothing OrElse mInvoker.IsDisposed Then Exit Sub
If mInvoker.InvokeRequired Then
Dim d As SetControlPropertyValueHandler = New SetControlPropertyValueHandler( AddressOf SetControlPropertyValue)
Try
mInvoker.Invoke(d, New Object () {ctr, propertyName, value, index})
Catch ex As Exception
End Try
Else
uReflection.MemberInfoFunction.SetPropertyValue(ctr, propertyName, value, index)
Application.DoEvents()
End If
ElseIf TypeOf ctr Is ToolStripItem Then
Dim mInvoker As ToolStrip = CType (ctr, ToolStripItem).GetCurrentParent
If mInvoker Is Nothing OrElse mInvoker.IsDisposed Then Exit Sub
If mInvoker.InvokeRequired Then
Dim d As SetControlPropertyValueHandler = New SetControlPropertyValueHandler( AddressOf SetControlPropertyValue)
Try
mInvoker.Invoke(d, New Object () {ctr, propertyName, value, index})
Catch ex As Exception
End Try
Else
uReflection.MemberInfoFunction.SetPropertyValue(ctr, propertyName, value, index)
Application.DoEvents()
End If
End If
End Sub
Public Shared Sub UpdateControlByCurrentClassAction( ByVal ctr As Object , ByVal action As Threading.ThreadStart)
If TypeOf ctr Is Control Then
Dim mInvoker As Control = CType (ctr, Control)
If mInvoker Is Nothing OrElse mInvoker.IsDisposed Then Exit Sub
If mInvoker.InvokeRequired Then
Dim d As UpdateControlByCurrentClassActionHandler = New UpdateControlByCurrentClassActionHandler( AddressOf UpdateControlByCurrentClassAction)
Try
mInvoker.Invoke(d, New Object () {ctr, action})
Catch ex As Exception
End Try
Else
action.Method.Invoke(ctr, Nothing )
End If
ElseIf TypeOf ctr Is ToolStripItem Then
Dim mInvoker As ToolStrip = CType (ctr, ToolStripItem).GetCurrentParent
If mInvoker Is Nothing OrElse mInvoker.IsDisposed Then Exit Sub
If mInvoker.InvokeRequired Then
Dim d As UpdateControlByCurrentClassActionHandler = New UpdateControlByCurrentClassActionHandler( AddressOf UpdateControlByCurrentClassAction)
Try
mInvoker.Invoke(d, New Object () {ctr, action})
Catch ex As Exception
End Try
Else
action.Method.Invoke(ctr, Nothing )
End If
End If
End Sub
Public Shared Sub UpdateControlByOtherClassAction( ByVal ctr As Object , ByVal actionObject As Object , ByVal action As Threading.ThreadStart)
If TypeOf ctr Is Control Then
Dim mInvoker As Control = CType (ctr, Control)
If mInvoker Is Nothing OrElse mInvoker.IsDisposed Then Exit Sub
If mInvoker.InvokeRequired Then
Dim d As UpdateControlByOtherClassActionHandler = New UpdateControlByOtherClassActionHandler( AddressOf UpdateControlByOtherClassAction)
Try
mInvoker.Invoke(d, New Object () {ctr, actionObject, action})
Catch ex As Exception
End Try
Else
action.Method.Invoke(actionObject, Nothing )
End If
ElseIf TypeOf ctr Is ToolStripItem Then
Dim mInvoker As ToolStrip = CType (ctr, ToolStripItem).GetCurrentParent
If mInvoker Is Nothing OrElse mInvoker.IsDisposed Then Exit Sub
If mInvoker.InvokeRequired Then
Dim d As UpdateControlByOtherClassActionHandler = New UpdateControlByOtherClassActionHandler( AddressOf UpdateControlByOtherClassAction)
Try
mInvoker.Invoke(d, New Object () {ctr, actionObject, action})
Catch ex As Exception
End Try
Else
action.Method.Invoke(actionObject, Nothing )
End If
End If
End Sub
Public Shared Sub StartActionByThreading( ByVal threadStart As Threading.ThreadStart, ByVal waiting As Boolean , ByRef abort As Boolean )
Dim t As New Threading.Thread(threadStart)
t.Start()
If waiting Then
While t.IsAlive
Windows.Forms.Application.DoEvents()
If abort Then
Try
t.Abort()
Catch ex As Exception
End Try
End If
End While
End If
abort = True
End Sub
Public Shared Sub StartActionByThreading( ByVal threadStart As Threading.ThreadStart)
StartActionByThreading(threadStart, False , False )
End Sub
Public Shared Sub UpdateControlByCurrentClassThreadAction( ByVal ctr As Object , ByVal action As Threading.ThreadStart, ByRef abort As Boolean )
SyncLock InternalSyncObject
gCtr = ctr
gAction = action
StartActionByThreading( AddressOf ThreadActionByCurrentClass, True , abort)
End SyncLock
End Sub
Public Shared Sub UpdateControlByCurrentClassThreadAction( ByVal ctr As Object , ByVal action As Threading.ThreadStart)
SyncLock InternalSyncObject
gCtr = ctr
gAction = action
StartActionByThreading( AddressOf ThreadActionByCurrentClass)
End SyncLock
End Sub
Private Shared Sub ThreadActionByCurrentClass()
UpdateControlByCurrentClassAction(gCtr, gAction)
End Sub
Public Shared Sub UpdateControlByOtherClassThreadAction( ByVal ctr As Object , ByVal actionObject As Object , ByVal action As Threading.ThreadStart, ByRef abort As Boolean )
SyncLock InternalSyncObject
gCtr = ctr
gAction = action
gActionInstance = actionObject
StartActionByThreading( AddressOf ThreadActionByOtherClass, True , abort)
End SyncLock
End Sub
Public Shared Sub UpdateControlByOtherClassThreadAction( ByVal ctr As Object , ByVal actionObject As Object , ByVal action As Threading.ThreadStart)
SyncLock InternalSyncObject
gCtr = ctr
gAction = action
gActionInstance = actionObject
StartActionByThreading( AddressOf ThreadActionByOtherClass)
End SyncLock
End Sub
Private Shared Sub ThreadActionByOtherClass()
UpdateControlByOtherClassAction(gCtr, gActionInstance, gAction)
End Sub
Private Shared ReadOnly Property InternalSyncObject() As Object
Get
If gInternalSyncObject Is Nothing Then
Dim tmpObj As New Object
System.Threading.Interlocked.CompareExchange(gInternalSyncObject, tmpObj, Nothing )
End If
Return gInternalSyncObject
End Get
End Property
Private Shared gInternalSyncObject As Object
Private Shared gCtr As Object
Private Shared gAction As Threading.ThreadStart
Private Shared gActionInstance As Object
End Class
End Namespace