programing

VB.NET에 새 스레드 생성

elecom 2023. 5. 18. 20:45
반응형

VB.NET에 새 스레드 생성

익명 함수를 사용하여 새 스레드를 생성하려고 하는데 오류가 계속 발생합니다.내 코드는 다음과 같습니다.

New Thread(Function() 
    'Do something here
End Function).Start

다음과 같은 오류가 발생합니다.

새로 만들기:

구문 오류

종료 기능:

'End Function'은 일치하는 'Function' 앞에 와야 합니다.

두 가지 방법이 있습니다.

  1. 와 함께AddressOf기존 메소드의 연산자

    Sub MyBackgroundThread()
      Console.WriteLine("Hullo")
    End Sub
    

    그런 다음 스레드를 만들고 시작합니다.

    Dim thread As New Thread(AddressOf MyBackgroundThread)
    thread.Start()
    
  2. 람다 함수로 사용할 수도 있습니다.

    Dim thread as New Thread(
      Sub() 
        Console.WriteLine("Hullo")
      End Sub
    )
    thread.Start()
    

VB에서는 람다 식이라고 합니다.구문이 모두 잘못되었습니다. New 연산자를 사용하려면 실제로 Thread 유형의 변수를 선언해야 합니다.그리고 생성하는 람다는 스레드 클래스 생성자에게 전달하는 인수의 유효한 대체 변수여야 합니다.값을 반환하는 대리자를 사용하지 않으므로 함수가 아닌 하위를 사용해야 합니다.랜덤 예제:

Imports System.Threading

Module Module1

    Sub Main()
        Dim t As New Thread(Sub()
                                Console.WriteLine("hello thread")
                            End Sub)
        t.Start()
        t.Join()
        Console.ReadLine()
    End Sub

End Module

서브가 아니라 함수여야 합니다.

한 줄(값을 반환해야 함):

Dim worker As New Thread(New ThreadStart(Function() 42))

다중 회선:

Dim worker As New Thread(New ThreadStart(Function()
                                                     ' Do something here
                                                 End Function))

출처: VB의 스레드화, 폐쇄 람다 식.그물

언급URL : https://stackoverflow.com/questions/10182563/create-a-new-thread-in-vb-net

반응형