相当于VB.NET中的C#BeginInvoke((Action))



我需要将以下C#代码转换为VB.NET:

if (this.InvokeRequired)
{
this.BeginInvoke((Action)(() =>
{
imageMutex.WaitOne();
pbCamera.Image = (Bitmap)imageCamera.Clone();
imageMutex.ReleaseMutex();
}));
}

我试过这样做:

If Me.InvokeRequired Then
Me.BeginInvoke((Action)(Function()
imageMutex.WaitOne()
pbCamera.Image = CType(imageCamera.Clone(), Bitmap)
imageMutex.ReleaseMutex()
))
End If

但编译器告诉我Action是一种类型,不能用作表达式。这样的委托是如何用VB.NET编写的?

直接翻译为:

If Me.InvokeRequired Then
Me.BeginInvoke(DirectCast(
Sub()
imageMutex.WaitOne()
pbCamera.Image = DirectCast(imageCamera.Clone(), Bitmap)
imageMutex.ReleaseMutex()
End Sub, 
Action)
)
End If

正如其他人所指出的,您不需要将lambda强制转换为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很好地转换了这个。如果你做了很多工作,在C#中找到了你想要的代码,但在转换的几个方面遇到了障碍,那么可能需要考虑一下

最新更新