我希望将两个或更多参数传递给VB 2008中的一个线程。
以下方法(已修改)在没有参数的情况下正常工作,我的状态栏会更新非常酷。 但我似乎无法使用一个,两个或更多参数。
这是按下按钮时我想到的伪代码:
Private Sub Btn_Click()
Dim evaluator As New Thread(AddressOf Me.testthread(goodList, 1))
evaluator.Start()
Exit Sub
这是testthread方法:
Private Sub testthread(ByRef goodList As List(Of OneItem), ByVal coolvalue As Integer)
StatusProgressBar.Maximum = 100000
While (coolvalue < 100000)
coolvalue = coolvalue + 1
StatusProgressBar.Value = coolvalue
lblPercent.Text = coolvalue & "%"
Me.StatusProgressBar.Refresh()
End While
End Sub
答案 0 :(得分:37)
首先:AddressOf
只是获取一个函数的委托 - 你不能指定任何其他东西(即捕获任何变量)。
现在,您可以通过两种可能的方式启动线程。
Action
,只在Start()
线程中传递。ParameterizedThreadStart
并将一个额外对象参数转发给调用.Start(parameter)
时指向的方法我认为后一种选择是前通用的,前lambda时代的一种时代错误 - 最迟在VB10结束。
您可以使用该粗略方法并创建一个列表或结构,您将其作为单个对象参数传递给线程代码,但是因为我们现在< / strong>有闭包,你可以在匿名Sub
上创建线程,它自己知道所有必要的变量(这是编译器为你执行的工作)。
Soo ......
Dim Evaluator = New Thread(Sub() Me.TestThread(goodList, 1))
真的就是那个;)
答案 1 :(得分:6)
像这样(我不是VB程序员)
Public Class MyParameters
public property Name As String
public property Number As Integer
End Class
Thread newThread = new Thread(AddressOf DoWork);
Dim parameters As New MyParameters
parameters.Name = "Arne"
newThread.Start(parameters);
public shared sub DoWork(byval data as object)
{
dim parameters = CType(data, Parameters)
}
答案 2 :(得分:4)
Dim evaluator As New Thread(Sub() Me.testthread(goodList, 1))
With evaluator
.IsBackground = True ' not necessary...
.Start()
End With
答案 3 :(得分:3)
嗯,直截了当的方法是创建一个适当的类/结构,它保存所有参数值并将其传递给线程。
VB10中的另一个解决方案是使用lambdas创建闭包的事实,这基本上意味着编译器会自动为您执行上述操作:
Dim evaluator As New Thread(Sub()
testthread(goodList, 1)
End Sub)
答案 4 :(得分:3)
除了Dario所说的代表之外,你还可以执行一个带有几个参数的委托:
预先定义您的代表:
Private Delegate Sub TestThreadDelegate(ByRef goodList As List(Of String), ByVal coolvalue As Integer)
获取委托的句柄,在数组中创建参数,在委托上创建DynamicInvoke:
Dim tester As TestThreadDelegate = AddressOf Me.testthread
Dim params(1) As Object
params(0) = New List(Of String)
params(1) = 0
tester.DynamicInvoke(params)
答案 5 :(得分:0)
只需创建一个包含两个成员的类或结构,一个List(Of OneItem)
,另一个Integer
,然后发送该类的实例。
编辑:抱歉,错过了您也遇到了一个参数问题。只需查看Thread Constructor (ParameterizedThreadStart),该页面就会包含一个简单的示例。
答案 6 :(得分:0)
我认为这会对你有所帮助...... 的 Creating Threads and Passing Data at Start Time 强>!
Imports System.Threading
' The ThreadWithState class contains the information needed for
' a task, and the method that executes the task.
Public Class ThreadWithState
' State information used in the task.
Private boilerplate As String
Private value As Integer
' The constructor obtains the state information.
Public Sub New(text As String, number As Integer)
boilerplate = text
value = number
End Sub
' The thread procedure performs the task, such as formatting
' and printing a document.
Public Sub ThreadProc()
Console.WriteLine(boilerplate, value)
End Sub
End Class
' Entry point for the example.
'
Public Class Example
Public Shared Sub Main()
' Supply the state information required by the task.
Dim tws As New ThreadWithState( _
"This report displays the number {0}.", 42)
' Create a thread to execute the task, and then
' start the thread.
Dim t As New Thread(New ThreadStart(AddressOf tws.ThreadProc))
t.Start()
Console.WriteLine("Main thread does some work, then waits.")
t.Join()
Console.WriteLine( _
"Independent task has completed main thread ends.")
End Sub
End Class
' The example displays the following output:
' Main thread does some work, then waits.
' This report displays the number 42.
' Independent task has completed; main thread ends.
答案 7 :(得分:0)
为VB.NET 3.5传递多个参数
<body>
<div>
click
</div>
</body>
<script>
window.onclick = function(e) {
if (e.target.tagName === "DIV") return false
// do something;
else console.log(this)
}
</script>