How to use BeginInvoke in VB.NET

孤街浪徒 提交于 2021-01-27 22:08:45

问题


In C# you use BeginInvoke like this:

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

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
)

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?


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.


回答1:


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

You have got the correct conversion.

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




回答2:


Yes, the (Action) (() => doesn't return anything so Sub in VB.Net is equivalent. It'd be a Func in C# if it did return something.



来源:https://stackoverflow.com/questions/37278917/how-to-use-begininvoke-in-vb-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!