■No33670に返信(魔界の仮面弁士さんの記事)
ご回答ありがとうございます
案2を実装しようと思い自分なりにコードをVBに変えたのですが文法エラーが直りません
書き方をご指摘していただけないでしょうか?
Public Class ThreadBuilder
Public Delegate Function MethodDelegate(Of T)() As T
Public Delegate Sub CallbackDelegate(Of T)(ReturnValue As T)
Public Shared Function NewThread(Of T)(Method As MethodDelegate(Of T), CallBack As CallbackDelegate(Of T)) As Threading.Thread
Return New Threading.Thread(New Threading.ParameterizedThreadStart( _
Sub()
~~~~~→Visual Basic 9.0はこのラムダ式をサポートしていません
Dim Ret As T = Method()
If CallBack IsNot Nothing Then CallBack(Ret)
End Sub))
End Function
End Class
Public Class Program
Sub Main(Args() As String)
Dim Val1 As Integer = 1
Dim Val2 As Integer = 2
Dim Ans As Integer = 0
Dim NewThread as ThreadBuilder.NewThread(of integer)( _
Sub()
~~~~~→配列の指定を、型指定子に記述することはできません。
Return Val1 + Val2
End Sub, _
Sub()
~~~~~→上と同じ
Ans = returnValue
End Sub
End Sub
End Class
Public Class ThreadBuilder Public Delegate Function MethodDelegate(Of T)() As T Public Delegate Sub CallbackDelegate(Of T)(ByVal ReturnValue As T)
Public Shared Function NewThread(Of T)(ByVal Method As MethodDelegate(Of T), ByVal CallBack As CallbackDelegate(Of T)) As Threading.Thread Dim act As New Worker(Of T)(Method, CallBack) Return New Threading.Thread(New Threading.ParameterizedThreadStart(AddressOf act.Invoke)) End Function
Private Class Worker(Of T) Private _Method As MethodDelegate(Of T) Private _CallBack As CallbackDelegate(Of T) Public Sub New(ByVal Method As MethodDelegate(Of T), ByVal CallBack As CallbackDelegate(Of T)) _Method = Method _CallBack = CallBack End Sub Public Sub Invoke() Dim Ret As T = _Method() If _CallBack IsNot Nothing Then _CallBack(Ret) End If End Sub End Class End Class
'-------------------------------------------------------- 'Module1.vb '-------------------------------------------------------- Imports System.Threading Module Module1 Sub Main() 'ラムダ式が使えないので、受け渡し用のクラスを用意しなければならない。 Dim wk As New Worker(1, 2)
'スレッド生成 Dim newThread As Thread = ThreadBuilder.NewThread(Of Integer)(AddressOf wk.Method, AddressOf wk.CallBack)
'スレッド開始 newThread.Start()
'メインスレッドの処理
'サブスレッドの完了待ち newThread.Join() Console.WriteLine(wk.ans) Console.ReadLine() End Sub
Private Class Worker Private val1, val2 As Integer Public ans As Integer
Public Sub New(ByVal val1 As Integer, ByVal val2 As Integer) Me.val1 = val1 Me.val2 = val2 End Sub
''' <summary> ''' 別スレッドに実行させたい処理 ''' </summary> Friend Function Method() As Integer Return Me.val1 + Me.val2 End Function
''' <summary> ''' スレッド完了後に戻り値をセットする処理 ''' </summary> Friend Sub CallBack(ByVal returnValue As Integer) Me.ans = returnValue End Sub End Class End Module
'-------------------------------------------------------- 'ThreadBuilder.vb '-------------------------------------------------------- Imports System.Threading Public Class ThreadBuilder Public Delegate Function MethodDelegate(Of T)() As T Public Delegate Sub CallbackDelegate(Of T)(ByVal ReturnValue As T)
Public Shared Function NewThread(Of T)(ByVal Method As MethodDelegate(Of T), ByVal CallBack As CallbackDelegate(Of T)) As Thread Dim act As New Worker(Of T)(Method, CallBack) Return New Thread(New Threading.ParameterizedThreadStart(AddressOf act.Invoke)) End Function
Private Class Worker(Of T) Private Method As MethodDelegate(Of T) Private Callback As CallbackDelegate(Of T) Public Sub New(ByVal Method As MethodDelegate(Of T)) Me.New(Method, Nothing) End Sub Public Sub New(ByVal Method As MethodDelegate(Of T), ByVal CallBack As CallbackDelegate(Of T)) Me.Method = Method Me.Callback = CallBack End Sub Public Sub Invoke(ByVal CallBack As Object) Dim cb As CallbackDelegate(Of T) = TryCast(CallBack, CallbackDelegate(Of T)) If cb Is Nothing Then cb = Me.Callback End If Invoke(cb) End Sub Private Sub Invoke(ByVal CallBack As CallbackDelegate(Of T)) Dim Ret As T = Method() If CallBack IsNot Nothing Then CallBack(Ret) End If End Sub End Class End Class