在VB.NET中创建一个新线程 [英] Create a new thread in VB.NET

查看:220
本文介绍了在VB.NET中创建一个新线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用匿名函数创建一个新线程,但我不断收到错误消息.这是我的代码:

I am trying to create a new thread using an anonymous function but I keep getting errors. Here is my code:

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

这是我得到的错误:

新功能:

语法错误

结束功能:

结束功能"之前必须有匹配的功能".

'End Function' must be preceded by a matching 'Function'.

推荐答案

有两种方法可以做到这一点;

There's two ways to do this;

  1. 使用AddressOf运算符将其转换为现有方法

  1. With the AddressOf operator to an existing method

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

然后使用创建并启动线程;

And then create and start the thread with;

Dim thread As New Thread(AddressOf MyBackgroundThread)
thread.Start()

  • 或作为lambda函数.

  • Or as a lambda function.

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

  • 这篇关于在VB.NET中创建一个新线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆