我试图使用Stephen Toub在这里找到的文章中的SocketAwaitable类; http://blogs.msdn.com/b/pfxteam/archive/2011/12/15/10248293.aspx
我已使用此处的DeveloperFusions工具将其转换为VB.NET; http://www.developerfusion.com/tools/convert/csharp-to-vb/
它给我的代码是;
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
但是,转换后出现了一些错误,我不确定如何修复。具体地说...
Private Shared ReadOnly SENTINEL As Action = Function()
End Function
...给我一个&#34;不能推断类型。考虑添加一个&#39; As&#39;子句指定返回类型&#34;。也...
AddHandler eventArgs.Completed, Sub()
Dim prev As action = If(m_continuation, Interlocked.CompareExchange(m_continuation, SENTINEL, Nothing))
RaiseEvent prev()
End Sub
...错误&#34;&#39; prev&#39;不是SocketAwaitable&#34;的事件。 (也许我可以删除RaiseEvent?)
有人可以帮我转换吗?
答案 0 :(得分:2)
尝试以下操作 - 删除RaiseEvent,空lambda应为&#39; Sub&#39;拉姆达:
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