Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 如何通过将变量名强制转换为对象来动态启动多个线程_Vb.net_Casting_Dynamically Generated - Fatal编程技术网

Vb.net 如何通过将变量名强制转换为对象来动态启动多个线程

Vb.net 如何通过将变量名强制转换为对象来动态启动多个线程,vb.net,casting,dynamically-generated,Vb.net,Casting,Dynamically Generated,不确定这将有多困难,或者是否涉及铸造,但这是我想要的(请使用vb.net代码) 我想要的只是一个循环,它将创建具有不同名称的不同线程 dim variableName="Thread" for i as Integer = 0 to 5 "dim " & variableName & (i) & "as new Threading.Thread" next 然后启动它们 for i as integer = 0 to 5 variableName &am

不确定这将有多困难,或者是否涉及铸造,但这是我想要的(请使用vb.net代码)

我想要的只是一个
循环
,它将创建具有不同名称的不同
线程

dim variableName="Thread"
for i as Integer = 0 to 5
    "dim " & variableName & (i) & "as new Threading.Thread"
next
然后启动它们

for i as integer = 0 to 5
    variableName & i.tostring" = New Thread(New ParameterizedThreadStart(AddressOf SubOrFunction))"
    variableName & i.tostring".Start(s)
  • 如何做到这一点
  • 这实际上叫什么(术语)

  • 提前感谢。

    通常,当您想要为对象动态分配名称时,最好使用字典。在字典中,您将为动态分配的名称使用键,并将该值用作分配该名称的对象。例如:

    Dim d As New Dictionary(Of String, MyClass)()
    
    'Add objects to dictionary
    d("dynamic name 1") = New MyClass()
    d("dynamic name 2") = New MyClass()
    
    'Get object from dictionary by name
    Dim myObject As MyClass = d("dynamic name 1")
    
    Dim threads As New Dictionary(Of String, Thread)()
    Dim variableName = "Thread"
    For i As Integer = 0 To 5
        threads(variableName & "(" & i.ToString() & ")") = New Thread()
    Next
    
    同样的方法也适用于线程,例如:

    Dim d As New Dictionary(Of String, MyClass)()
    
    'Add objects to dictionary
    d("dynamic name 1") = New MyClass()
    d("dynamic name 2") = New MyClass()
    
    'Get object from dictionary by name
    Dim myObject As MyClass = d("dynamic name 1")
    
    Dim threads As New Dictionary(Of String, Thread)()
    Dim variableName = "Thread"
    For i As Integer = 0 To 5
        threads(variableName & "(" & i.ToString() & ")") = New Thread()
    Next
    
    但是,如果您所做的只是为它们指定数字索引,而不是字符串名称,则可以使用列表,如下所示:

    Dim threads As New List(Of Thread)()
    For i As Integer = 0 To 5
        threads.Add(New Thread())
    Next
    
    Dim t As Thread = threads(1)
    
    然后,您可以通过如下索引从列表中获取线程:

    Dim threads As New List(Of Thread)()
    For i As Integer = 0 To 5
        threads.Add(New Thread())
    Next
    
    Dim t As Thread = threads(1)
    
    或者,如果您有一组线程,则可以轻松使用数组:

    Dim threads(4) As Thread
    For i As Integer = 0 To 5
        threads(i) = New Thread()
    Next
    
    创建一个
    命令(字符串、线程)
    ,以便轻松访问它们

    Dim dictThread as new Dictionary(of String, Thread)
    For i as integer = 0 to 5
       dictThread.add("Thread" & i.toString, New Thread(New ParameterizedThreadStart(AddressOf SubOrFunction))
    Next
    
    开始吧

    For Each t as Thread in dictThread.Values
       t.Start(WhateverSIs)
    Next
    
    或启动特定线程:
    dictThread(“Thread3”)。启动

    注:仅编写,未测试;)