摘要:ClassFunctionHelper - parallel function.

"nsThread"#Region "nsThread"
Namespace nsThread
Public Module mThread 
Public Sub PrintOnePDFReport() Sub PrintOnePDFReport(ByVal psLocalFile As String, Optional ByVal psCount As Integer = 0)
Try
Dim ClsThreadPrintOnePDF As New ClassThreadPrintOnePDF
ClsThreadPrintOnePDF.PrintOnePDFReport_Master(psLocalFile, psCount)
Catch Err As Exception
Call ErrHandler(Err.Message, APP_NAME)
End Try
End Sub 
Public Sub ParallelFunction() Sub ParallelFunction(ByVal pForm As Form, ByVal pSub0 As DSub0)
Try
Dim ClsFunctionHelper As New ClassFunctionHelper
ClsFunctionHelper.SetForm(pForm)
ClsFunctionHelper.SetSub0(pSub0)
ClsFunctionHelper.StartThreadFunction()
Catch Err As Exception
Call ErrHandler(Err.Message, APP_NAME)
End Try
End Sub 
Public Sub ParallelFunctionWithOutInvoke() Sub ParallelFunctionWithOutInvoke(ByVal pSub0 As DSub0)
Try
Dim ClsFunctionHelper As New ClassFunctionHelperBasic
ClsFunctionHelper.SetSub0(pSub0)
ClsFunctionHelper.StartThreadFunctionWithOutInvoke()
Catch Err As Exception
Call ErrHandler(Err.Message, APP_NAME)
End Try
End Sub 
Public Sub InvokeFunction() Sub InvokeFunction(ByVal pForm As Form, ByVal pSub0 As DSub0)
Try
Dim ClsFunctionHelper As New ClassFunctionHelper
ClsFunctionHelper.SetForm(pForm)
ClsFunctionHelper.SetSub0(pSub0)
ClsFunctionHelper.InvokeFunction()
Catch Err As Exception
Call ErrHandler(Err.Message, APP_NAME)
End Try
End Sub 
Public Sub InvokeFunction() Sub InvokeFunction(ByVal pForm As Form, ByVal pSub1 As DSub1, ByVal pParameter As Object)
Try
Dim ClsFunctionHelper As New ClassFunctionHelper
ClsFunctionHelper.SetForm(pForm)
ClsFunctionHelper.SetSub1(pSub1)
ClsFunctionHelper.SetParameter(pParameter)
ClsFunctionHelper.InvokeFunction()
Catch Err As Exception
Call ErrHandler(Err.Message, APP_NAME)
End Try
End Sub
End Module
End Namespace
#End Region

" ClassFunctionHelper "#Region " ClassFunctionHelper "
Public Class ClassFunctionHelper
Inherits ClassFunctionHelperBasic

Delegate Sub CallbackFunction() CallbackFunction()
Private mForm As Form

Public Sub SetForm() Sub SetForm(ByVal pForm As Form)
Try
Me.mForm = pForm
Catch Err As Exception
Call ErrHandler(Err.Message, APP_NAME)
End Try
End Sub

Public Sub InvokeFunction() Sub InvokeFunction()
Try
If mForm.InvokeRequired Then
Dim CallBackForTest As New CallbackFunction(AddressOf InvokeFunction)
mForm.Invoke(CallBackForTest)
Else
MyBase.DSub()
End If
Catch Err As Exception
Call ErrHandler(Err.Message, APP_NAME)
End Try
End Sub 
Public Sub StartThreadFunction() Sub StartThreadFunction()
Try
Dim lThread As Thread = New Thread(New ThreadStart(AddressOf InvokeFunction))
lThread.Start()
Catch Err As Exception
Call ErrHandler(Err.Message, APP_NAME)
End Try
End Sub
End Class
Public Class ClassFunctionHelperBasic
Protected mDSub0 As DSub0
Protected mDSub1 As DSub1
Private mObject As Object

Public Sub SetSub1() Sub SetSub1(ByVal pDSub1 As DSub1)
Try
Me.mDSub1 = pDSub1
Catch Err As Exception
Call ErrHandler(Err.Message, APP_NAME)
End Try
End Sub

Public Sub SetParameter() Sub SetParameter(ByVal pObject As Object)
Try
mObject = pObject
Catch Err As Exception
Call ErrHandler(Err.Message, APP_NAME)
End Try
End Sub

Public Sub SetSub0() Sub SetSub0(ByVal pDSub0 As DSub0)
Try
Me.mDSub0 = pDSub0
Catch Err As Exception
Call ErrHandler(Err.Message, APP_NAME)
End Try
End Sub

Protected Sub DSub() Sub DSub()
Try
If Me.mDSub0 Is Nothing Then
Me.mDSub1(Me.mObject)
Else
Me.mDSub0()
End If
Catch Err As Exception
Call ErrHandler(Err.Message, APP_NAME)
End Try
End Sub

Public Sub StartThreadFunctionWithOutInvoke() Sub StartThreadFunctionWithOutInvoke()
Try
Dim lThread As Thread = New Thread(New ThreadStart(AddressOf DSub))
lThread.Start()
Catch Err As Exception
Call ErrHandler(Err.Message, APP_NAME)
End Try
End Sub
End Class
#End Region
------------------
熱愛生命 喜愛新奇 有趣的事物
過去 是無法改變
將來 卻能夠創造
希望使大家生活更便利
世界更美好
a guy who loves IT and life