Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将这个SocketAwaitable类从C#转换为VB?_C#_.net_Vb.net - Fatal编程技术网

如何将这个SocketAwaitable类从C#转换为VB?

如何将这个SocketAwaitable类从C#转换为VB?,c#,.net,vb.net,C#,.net,Vb.net,我试图使用Stephen Toub文章中的SocketAwaitable类; 我已经用这里的DeveloperFusions工具将其转换为VB.NET; 它给我的代码是 Public NotInheritable Class SocketAwaitable Implements INotifyCompletion Private Shared ReadOnly SENTINEL As Action = Function()

我试图使用Stephen Toub文章中的SocketAwaitable类;

我已经用这里的DeveloperFusions工具将其转换为VB.NET;

它给我的代码是

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
。。。给我一个“不能推断类型”。考虑添加一个“AS”子句来指定返回类型。还有

AddHandler eventArgs.Completed, Sub()
                                          Dim prev As action = If(m_continuation, Interlocked.CompareExchange(m_continuation, SENTINEL, Nothing))
                                          RaiseEvent prev()
                                        End Sub
。。。带有“'prev'不是SocketAwaitable事件”的错误。(也许我可以移除RaiseEvent?)


是否有人可以帮助我进行此转换?

尝试以下操作-删除RaiseEvent,空lambda应为“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
Public不可继承类SocketAwaitable
实现INotifyCompletion
私有只读共享SENTINEL As Action=Sub()
端接头
Friend m_以布尔形式完成
朋友m_继续行动
好友m_事件args作为SocketAsyncEventArgs
Public Sub New(ByVal事件参数作为SocketAsyncEventArgs)
如果eventArgs什么都不是,那么
抛出新ArgumentNullException(“eventArgs”)
如果结束
m_eventArgs=eventArgs
AddHandler eventArgs.Completed,Sub()
Dim prev=If(m_延续,联锁。比较交换(m_延续,哨兵,无))
如果prev不是空的,那么
prev()
如果结束
端接头
端接头
好友子重置()
m_wasCompleted=False
m_continuation=无
端接头
作为SocketAwaitable的公共函数GetAwaiter()
还我
端函数
公共只读属性IsCompleted()为布尔值
得到
返回m_已完成
结束
端属性
公共子项未完成(ByVal继续作为操作)
如果m_continuation是SENTINEL或LSE联锁的,则CompareExchange(m_continuation,continuation,Nothing)是SENTINEL
Task.Run(续)
如果结束
端接头
公共子GetResult()
如果m_eventArgs.SocketError SocketError.Success那么
抛出新的SocketException(CInt(Math.Truncate(m_eventArgs.SocketError)))
如果结束
端接头
末级

Action=
Sub
,Func=
Function
。尝试新操作(Sub())
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