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

问题描述 投票:-1回答:1

我需要将以下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编写这样的代表将如何?

c# vb.net anonymous-function code-translation begininvoke
1个回答
3
投票

直接翻译是:

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

© www.soinside.com 2019 - 2024. All rights reserved.