iguanaman
iguanaman

Reputation: 1030

How can I convert this SocketAwaitable class from C# to VB?

I'm trying to use the SocketAwaitable class from Stephen Toub's article found here; http://blogs.msdn.com/b/pfxteam/archive/2011/12/15/10248293.aspx

I've converted it to VB.NET with DeveloperFusions tool found here; http://www.developerfusion.com/tools/convert/csharp-to-vb/

The code it gave me is;

Public NotInheritable Class SocketAwaitable
  Implements INotifyCompletion
  Private Shared ReadOnly SENTINEL As Action = Function()

                                               End Function

  Friend m_wasCompleted As Boolean
  Friend m_continuation As Action
  Friend m_eventArgs As SocketAsyncEventArgs

  Public Sub New(eventArgs As SocketAsyncEventArgs)
    If eventArgs Is Nothing Then
      Throw New ArgumentNullException("eventArgs")
    End If
    m_eventArgs = eventArgs
    AddHandler eventArgs.Completed, Sub()
                                      Dim prev As action = If(m_continuation, Interlocked.CompareExchange(m_continuation, SENTINEL, Nothing))
                                      RaiseEvent prev()
                                    End Sub
  End Sub

  Friend Sub Reset()
    m_wasCompleted = False
    m_continuation = Nothing
  End Sub

  Public Function GetAwaiter() As SocketAwaitable
    Return Me
  End Function

  Public ReadOnly Property IsCompleted() As Boolean
    Get
      Return m_wasCompleted
    End Get
  End Property

  Public Sub OnCompleted(continuation As Action) Implements INotifyCompletion.OnCompleted
    If m_continuation = SENTINEL OrElse Interlocked.CompareExchange(m_continuation, continuation, Nothing) = SENTINEL Then
      Tasks.Task.Run(continuation)
    End If
  End Sub

  Public Sub GetResult()
    If m_eventArgs.SocketError <> SocketError.Success Then
      Throw New SocketException(CInt(m_eventArgs.SocketError))
    End If
  End Sub
End Class

However, there were a couple of errors after the conversion that I'm not sure how to fix. Specifically...

Private Shared ReadOnly SENTINEL As Action = Function()

                                                   End Function

... gives me a "Cannot infer type. Consider adding an 'As' clause to specify the return type". Also...

AddHandler eventArgs.Completed, Sub()
                                          Dim prev As action = If(m_continuation, Interlocked.CompareExchange(m_continuation, SENTINEL, Nothing))
                                          RaiseEvent prev()
                                        End Sub

... errors with "'prev' is not an event of SocketAwaitable". (Perhaps I can just remove the RaiseEvent?)

Can anyone help me with this conversion, please?

Upvotes: 0

Views: 148

Answers (1)

Dave Doknjas
Dave Doknjas

Reputation: 6542

Try the following - remove the RaiseEvent and the empty lambda should be a 'Sub' lambda:

Public NotInheritable Class SocketAwaitable
    Implements INotifyCompletion

    Private ReadOnly Shared SENTINEL As Action = Sub()
    End Sub

    Friend m_wasCompleted As Boolean
    Friend m_continuation As Action
    Friend m_eventArgs As SocketAsyncEventArgs

    Public Sub New(ByVal eventArgs As SocketAsyncEventArgs)
        If eventArgs Is Nothing Then
            Throw New ArgumentNullException("eventArgs")
        End If
        m_eventArgs = eventArgs
        AddHandler eventArgs.Completed, Sub()
            Dim prev = If(m_continuation, Interlocked.CompareExchange(m_continuation, SENTINEL, Nothing))
            If prev IsNot Nothing Then
                prev()
            End If
        End Sub
    End Sub

    Friend Sub Reset()
        m_wasCompleted = False
        m_continuation = Nothing
    End Sub

    Public Function GetAwaiter() As SocketAwaitable
        Return Me
    End Function

    Public ReadOnly Property IsCompleted() As Boolean
        Get
            Return m_wasCompleted
        End Get
    End Property

    Public Sub OnCompleted(ByVal continuation As Action)
        If m_continuation Is SENTINEL OrElse Interlocked.CompareExchange(m_continuation, continuation, Nothing) Is SENTINEL Then
            Task.Run(continuation)
        End If
    End Sub

    Public Sub GetResult()
        If m_eventArgs.SocketError <> SocketError.Success Then
            Throw New SocketException(CInt(Math.Truncate(m_eventArgs.SocketError)))
        End If
    End Sub
End Class

Upvotes: 2

Related Questions