LzmTW.uSystem.uThreading+CrossThread

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值