我需要将以下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:
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#中找到想要的代码但在转换的两个方面都遇到了麻烦,那么您可能需要考虑很多事情