如何在 VB.NET 中使用 BeginInvoke [英] How to use BeginInvoke in VB.NET

查看:81
本文介绍了如何在 VB.NET 中使用 BeginInvoke的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 C# 中,您像这样使用 BeginInvoke:

In C# you use BeginInvoke like this:

obj.BeginInvoke((Action)(() =>
{
    //do something
}));

我试图将它翻译成 VB.NET,最后得到了这段代码,这似乎有效:

I tried to translate it to VB.NET, and ended up with this code, that seems to work:

obj.BeginInvoke(
    Sub()
        'do something'
    End Sub
)

这些片段在我看来非常不同,尤其是因为 (Action) (() => 部分完全缺失.这是使用 BeginInvoke 的正确方法吗?在 VB.NET 中?

The snippets look very different to me, especially because the (Action) (() => part is missing completely. Is this the correct way to use BeginInvoke in VB.NET?

不是重复如何使用BeginInvokeC# 因为问题和每个答案都使用 C#(如果使用任何编程语言).当您不确定是否使用了正确的 VB.NET 语法时,C# 代码没有多大帮助.

this is not a duplicate of How to use BeginInvoke C# because the question and every answer uses C# (if any programming language is used). C#-code doesn't help much when you are unsure about if you used the correct VB.NET syntax.

推荐答案

(Action) 只是将 lambda 转换为 Action,这在 VB 中是不需要的.网.Sub() lambda 就是你所需要的.

(Action) just casts the lambda to an Action, which isn't needed in VB.NET. The Sub() lambda is all you need.

您得到了正确的转换.

虽然注意BeginInvoke()后面一定要跟EndInvoke(),否则会出现线程泄漏.

Although note that BeginInvoke() must be followed by EndInvoke(), otherwise you will get thread leaks.

这篇关于如何在 VB.NET 中使用 BeginInvoke的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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