Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use BeginInvoke in VB.NET

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.

like image 907
Breeze Avatar asked Oct 19 '25 10:10

Breeze


1 Answers

(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.

like image 130
Visual Vincent Avatar answered Oct 21 '25 22:10

Visual Vincent



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!