【问题标题】:Converting C# ThreadStart Delegate to VB.NET将 C# ThreadStart 委托转换为 VB.NET
【发布时间】:2013-09-09 05:54:58
【问题描述】:

我需要将 C# 代码转换为 VB.NET 代码(基于 .NET 3.5 和 VS 2008),但我在将 C# 委托转换为其 VB.NET 等效代码时遇到问题。

我要转换的 C# 代码可以正常工作,在这里:

protected override void OnOwnerInitialized()
{
    if (!MobileApplication.Current.Dispatcher.CheckAccess())
    {
        // MobileApplication.Current is some 3rd party API
        // MobileApplication.Current.Dispatcher is type System.Windows.Threading.Dispatcher    
        MobileApplication.Current.Dispatcher.BeginInvoke
        (
            (System.Threading.ThreadStart)delegate()
            {
                OnOwnerInitialized();
            }
        );
        return;
    }

    DoSomething();
}

我翻译成以下 VB.NET 代码但不起作用:

Protected Overrides Sub OnOwnerInitialized()
    If Not MobileApplication.Current.Dispatcher.CheckAccess() Then
        MobileApplication.Current.Dispatcher.BeginInvoke(Function() New System.Threading.ThreadStart(AddressOf OnOwnerInitialized))
        Return
    End If

    ' I also tried the following but I get thread related errors elsewhere in code
    'If ((Not MobileApplication.Current.Dispatcher.CheckAccess()) And (Not threadStarted)) Then
    '    Dim newThread As New Thread(AddressOf OnOwnerInitialized)
    '    newThread.Start()
    '    threadStarted = True ' this is a shared / static variable
    '    Return
    'End If    

    DoSomething()
End Sub

使用 C# 代码, OnOwnerInitialized() 被调用两次。在第一次调用时,该函数以“return;”存在陈述;第二次调用 'DoSomething()。这是正确的行为。但是,在 VB.NET 代码中,它只运行一次,代码在“Return”语句上返回,就是这样(我相信我翻译的 VB.NET 代码没有正确调用线程。下面的代码是 C#代码)。

谢谢。

【问题讨论】:

    标签: c# vb.net multithreading delegates


    【解决方案1】:

    看起来你可以缩短它。

    http://msdn.microsoft.com/en-us/library/57s77029(v=vs.100).aspx?appId=Dev10IDEF1&l=EN-US&k=k(SYSTEM.THREADING.THREADSTART)%3bk(VS.OBJECTBROWSER)%3bk(TargetFrameworkMoniker-".NETFRAMEWORK,VERSION%3dV4.0")&rd=true&cs-save-lang=1&cs-lang=vb#code-sn-p-2

    Protected Overrides Sub OnOwnerInitialized()
        If Not MobileApplication.Current.Dispatcher.CheckAccess() Then
            MobileApplication.Current.Dispatcher.BeginInvoke(AddressOf OnOwnerInitialized)
            Return
        End If
    
        DoSomething()
    End Sub
    

    【讨论】:

    • 感谢您的快速回复...不幸的是它没有工作,因为我收到一个编译错误“AddressOf 表达式无法转换为 System.Delegate...”。我想这可能是因为我使用的是 .NET 3.5,它不支持这种简写。
    【解决方案2】:

    我想我明白了:

    Protected Overrides Sub OnOwnerInitialized()
        If Not MobileApplication.Current.Dispatcher.CheckAccess() Then
            MobileApplication.Current.Dispatcher.BeginInvoke(DirectCast(Function() DoSomethingWrapper(), System.Threading.ThreadStart))
            Return
        End If
    End If
    
    
    Private Function DoSomethingWrapper() As Boolean
        DoSomething()
        Return True ' dummy value to satisfy that this is a function
    End If
    

    基本上,我认为因为我使用的是.NET 3.5,所以下面这行只能取一个函数,不能取一个子:

        MobileApplication.Current.Dispatcher.BeginInvoke(DirectCast(Function() RunExtension(), System.Threading.ThreadStart))
    

    现在因为 OnOwnerInitialized() 必须是一个 sub(因为它覆盖了一个 sub),并且 BeginInvoke 必须采用一个函数,所以我只是添加了一个包装函数来包装 DoSomething(),因此 OnOwnerInitialized() 可以调用 DoSomething( ) 通过该包装函数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多