Hola
Estoy intentando crear una función genérica para crear Threads.
El problema es que no consigo pasarle a la función el nombre de un Sub como argumento, para poder asignarlo como el Sub del nuevo thread que se va a crear,
sólamente he llegado a conseguir pasarle el nombre de una función, lo cual no me sirve para nada, porque según me dice la IDE el procedimiento de un thread debe ser un Sub, no una función.
¿Tienen idea de como puedo conseguir resolverlo?
Me gustaría poder pasarle el nombre del Sub a la función sin tener que declarar un delegado fuera de la función, porque mi intención es hacer una función genérica que acepte cualquier nombre de un Sub como argumento, para simplificar las cosas.
El code:
Public Class Form1
' Desired usage:
' Make_Thread(Thread Variable, Thread Name, Thread Priority, Thread Sub)
'
' Example:
' Make_Thread(Thread_1, "Low thread", Threading.ThreadPriority.Low, AddressOf Test_Sub)
Private Thread_1 As Threading.Thread ' Thread Variable
Private Function Make_Thread(Of TKey)(ByRef Thread_Variable As Threading.Thread, _
ByVal Thread_Name As String, _
ByVal Thread_Priority As Threading.ThreadPriority, _
ByRef Thread_Sub As Func(Of String, TKey)) As Boolean ' I'd ByRef a function but really I will pass a Subroutine.
' See if the thread is already running.
Dim is_running As Boolean
If Thread_Variable Is Nothing Then _
is_running = False _
Else is_running = Thread_Variable.IsAlive
' If the thread is already running, do nothing.
If is_running Then Return False : Exit Function
' Make the thread.
Thread_Variable = New Threading.Thread(AddressOf Thread_Sub)
Thread_Variable.Priority = Thread_Priority
Thread_Variable.IsBackground = True
Thread_Variable.Name = Thread_Name
' Start the thread.
Thread_Variable.Start()
Return True
End Function
Private Sub Test_Sub()
MsgBox("A message from the thread!")
End Sub
End Class
¿Nadie sabe como se puede hacer?