【发布时间】:2015-09-23 15:54:25
【问题描述】:
如何在 VB.NET 中使用动作对象启动新线程?
_taskopts.AddTask(1, AddressOf MyMethod, "Test Task", True)
...
Public Sub AddTask(minfreq As Double, subroutine As Action, name As String, immediate As Boolean)
If minfreq > 0 Then
Dim task As New TaskObj With {.minfreq = minfreq, .subroutine = subroutine, .name = name}
If immediate Then
task.lastrun = New Date(1970, 1, 1)
Else
task.lastrun = Now()
End If
_tasks.Add(task)
End If
End Sub
Public Sub ExecuteTasks()
For Each task As TaskObj In _tasks
Try
TaskOp(task)
Catch ex As Exception
InvokeMessage("TaskOpts Execute Error: " & ex.Message)
End Try
Next
End Sub
Private Sub TaskOp(ByRef task As TaskObj)
If Now.Subtract(task.lastrun).TotalMinutes > task.minfreq Then
task.lastrun = Now()
Dim taskthread As New Thread(AddressOf task.subroutine) '*****ERROR HERE*****
taskthread.Start()
Thread.Sleep(100)
End If
End Sub
这会导致错误:“AddressOf 操作数必须是方法的名称。”我在想也许我需要使用 Action 对象以外的东西,也许是通过 AddTask 子传递 AddressOf 的某种方式。
【问题讨论】:
标签: .net vb.net multithreading