I need to convert the following C# code to VB.NET:
if (this.InvokeRequired)
{
this.BeginInvoke((Action)(() =>
{
imageMutex.WaitOne();
pbCamera.Image = (Bitmap)imageCamera.Clone();
imageMutex.ReleaseMutex();
}));
}
I have tried it like this:
If Me.InvokeRequired Then
Me.BeginInvoke((Action)(Function()
imageMutex.WaitOne()
pbCamera.Image = CType(imageCamera.Clone(), Bitmap)
imageMutex.ReleaseMutex()
))
End If
But the compiler tells me that Action
is a type and can not be used as an expression.
How would such a delegate by written in VB.NET?
The direct translation is:
If Me.InvokeRequired Then
Me.BeginInvoke(DirectCast(
Sub()
imageMutex.WaitOne()
pbCamera.Image = DirectCast(imageCamera.Clone(), Bitmap)
imageMutex.ReleaseMutex()
End Sub,
Action)
)
End If
As others have pointed out, you don't need to cast a lambda to an Action:
If Me.InvokeRequired Then
Me.BeginInvoke(
Sub()
imageMutex.WaitOne()
pbCamera.Image = DirectCast(imageCamera.Clone(), Bitmap)
imageMutex.ReleaseMutex()
End Sub
)
End If
https://codeconverter.icsharpcode.net had a good stab at converting this. Might be something to consider if youre doing a lot of work where you're finding the code you want in C# but snagging on a couple of aspects of the conversion