首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将c#代码转换为VB.NET - PingExtensions.cs示例的问题

将c#代码转换为VB.NET - PingExtensions.cs示例的问题
EN

Stack Overflow用户
提问于 2011-08-04 05:24:39
回答 1查看 593关注 0票数 0

嗨,我已经把这个并行扩展c#代码示例转换为VB.NET。

http://code.msdn.microsoft.com/Samples-for-Parallel-b4b76364/sourcecode?fileId=25353&pathId=215900242

使用开发融合工具这里,但是我得到了许多错误,我无法用有限的C#经验来解决这些错误。

1)在得到错误后,我将System.Runtime.CompilerServices.Extension转换为Global.System.Runtime.CompilerServices.ExtensionAttribute,这是我能想到的最接近的,我在行(26)上得到了错误。

代码语言:javascript
复制
Return SendTaskCore(ping, userToken, Function(tcs) ping.SendAsync(address, tcs))

ping.SendAsync(address, timeout, tcs)不会产生值

( 2)围绕第196行

代码语言:javascript
复制
handler = Function(sender, e) EAPCommon.HandleCompletion(tcs, e, Function() e.Reply, Function() ping.PingCompleted -= handler)

我在“ping.PingCompleted”一词上说错了

‘公共事件PingCompleted(发送方作为对象,e作为事件PingCompleted,不能直接调用。使用“RaiseEvent”语句引发事件。

如有任何建议,将不胜感激。完整代码如下(注释删除),原始源代码

http://code.msdn.microsoft.com/Samples-for-Parallel-b4b76364/sourcecode?fileId=25353&pathId=215900242

代码语言:javascript
复制
using System.Threading.Tasks; 

namespace System.Net.NetworkInformation 
{ 
    /// <summary>Extension methods for working with Ping asynchronously.</summary> 
    public static class PingExtensions 
    { 

        public static Task<PingReply> SendTask(this Ping ping, IPAddress address, object userToken) 
        { 
            return SendTaskCore(ping, userToken, tcs => ping.SendAsync(address, tcs)); 
        } 


        public static Task<PingReply> SendTask(this Ping ping, string hostNameOrAddress, object userToken) 
        { 
            return SendTaskCore(ping, userToken, tcs => ping.SendAsync(hostNameOrAddress, tcs)); 
        } 


        public static Task<PingReply> SendTask(this Ping ping, IPAddress address, int timeout, object userToken) 
        { 
            return SendTaskCore(ping, userToken, tcs => ping.SendAsync(address, timeout, tcs)); 
        } 


        public static Task<PingReply> SendTask(this Ping ping, string hostNameOrAddress, int timeout, object userToken) 
        { 
            return SendTaskCore(ping, userToken, tcs => ping.SendAsync(hostNameOrAddress, timeout, tcs)); 
        } 


        public static Task<PingReply> SendTask(this Ping ping, IPAddress address, int timeout, byte[] buffer, object userToken) 
        { 
            return SendTaskCore(ping, userToken, tcs => ping.SendAsync(address, timeout, buffer, tcs)); 
        } 


        public static Task<PingReply> SendTask(this Ping ping, string hostNameOrAddress, int timeout, byte[] buffer, object userToken) 
        { 
            return SendTaskCore(ping, userToken, tcs => ping.SendAsync(hostNameOrAddress, timeout, buffer, tcs)); 
        } 


        public static Task<PingReply> SendTask(this Ping ping, IPAddress address, int timeout, byte[] buffer, PingOptions options, object userToken) 
        { 
            return SendTaskCore(ping, userToken, tcs => ping.SendAsync(address, timeout, buffer, options, tcs)); 
        } 


        public static Task<PingReply> SendTask(this Ping ping, string hostNameOrAddress, int timeout, byte[] buffer, PingOptions options, object userToken) 
        { 
            return SendTaskCore(ping, userToken, tcs => ping.SendAsync(hostNameOrAddress, timeout, buffer, options, tcs)); 
        } 


        private static Task<PingReply> SendTaskCore(Ping ping, object userToken, Action<TaskCompletionSource<PingReply>> sendAsync) 
        { 
            // Validate we're being used with a real smtpClient.  The rest of the arg validation 
            // will happen in the call to sendAsync. 
            if (ping == null) throw new ArgumentNullException("ping"); 

            // Create a TaskCompletionSource to represent the operation 
            var tcs = new TaskCompletionSource<PingReply>(userToken); 

            // Register a handler that will transfer completion results to the TCS Task 
            PingCompletedEventHandler handler = null; 
            handler = (sender, e) => EAPCommon.HandleCompletion(tcs, e, () => e.Reply, () => ping.PingCompleted -= handler); 
            ping.PingCompleted += handler; 

            // Try to start the async operation.  If starting it fails (due to parameter validation) 
            // unregister the handler before allowing the exception to propagate. 
            try 
            { 
                sendAsync(tcs); 
            } 
            catch(Exception exc) 
            { 
                ping.PingCompleted -= handler; 
                tcs.TrySetException(exc); 
            } 

            // Return the task to represent the asynchronous operation 
            return tcs.Task; 
        } 
    } 
} 

编辑:这是转换后的VB代码:

代码语言:javascript
复制
Imports System.Threading.Tasks
Imports System.Runtime.CompilerServices
Imports System.Net.NetworkInformation
Imports System.Net
Imports System.ComponentModel



Namespace System.Net.NetworkInformation


    ''' <summary>Extension methods for working with Ping asynchronously.</summary> 
    Public Module PingExtensions
        Sub New()
        End Sub

        <Global.System.Runtime.CompilerServices.ExtensionAttribute()> _
        Public Function SendTask(ByVal ping As Ping, ByVal address As IPAddress, ByVal userToken As Object) As Task(Of PingReply)
            Return SendTaskCore(ping, userToken, Function(tcs) ping.SendAsync(address, tcs))
        End Function


        <ExtensionAttribute()> _
        Public Function SendTask(ByVal ping As Ping, ByVal hostNameOrAddress As String, ByVal userToken As Object) As Task(Of PingReply)
            Return SendTaskCore(ping, userToken, Function(tcs) ping.SendAsync(hostNameOrAddress, tcs))
        End Function


        <ExtensionAttribute()> _
        Public Function SendTask(ByVal ping As Ping, ByVal address As IPAddress, ByVal timeout As Integer, ByVal userToken As Object) As Task(Of PingReply)
            Return SendTaskCore(ping, userToken, Function(tcs) ping.SendAsync(address, timeout, tcs))
        End Function


        <ExtensionAttribute()> _
        Public Function SendTask(ByVal ping As Ping, ByVal hostNameOrAddress As String, ByVal timeout As Integer, ByVal userToken As Object) As Task(Of PingReply)
            Return SendTaskCore(ping, userToken, Function(tcs) ping.SendAsync(hostNameOrAddress, timeout, tcs))
        End Function


        <ExtensionAttribute()> _
        Public Function SendTask(ByVal ping As Ping, ByVal address As IPAddress, ByVal timeout As Integer, ByVal buffer As Byte(), ByVal userToken As Object) As Task(Of PingReply)
            Return SendTaskCore(ping, userToken, Function(tcs) ping.SendAsync(address, timeout, buffer, tcs))
        End Function


        <ExtensionAttribute()> _
        Public Function SendTask(ByVal ping As Ping, ByVal hostNameOrAddress As String, ByVal timeout As Integer, ByVal buffer As Byte(), ByVal userToken As Object) As Task(Of PingReply)
            Return SendTaskCore(ping, userToken, Function(tcs) ping.SendAsync(hostNameOrAddress, timeout, buffer, tcs))
        End Function


        <ExtensionAttribute()> _
        Public Function SendTask(ByVal ping As Ping, ByVal address As IPAddress, ByVal timeout As Integer, ByVal buffer As Byte(), ByVal options As PingOptions, ByVal userToken As Object) As Task(Of PingReply)
            Return SendTaskCore(ping, userToken, Function(tcs) ping.SendAsync(address, timeout, buffer, options, tcs))
        End Function


        <ExtensionAttribute()> _
        Public Function SendTask(ByVal ping As Ping, ByVal hostNameOrAddress As String, ByVal timeout As Integer, ByVal buffer As Byte(), ByVal options As PingOptions, ByVal userToken As Object) As Task(Of PingReply)
            Return SendTaskCore(ping, userToken, Function(tcs) ping.SendAsync(hostNameOrAddress, timeout, buffer, options, tcs))
        End Function


        Private Function SendTaskCore(ByVal ping As Ping, ByVal userToken As Object, ByVal sendAsync As Action(Of TaskCompletionSource(Of PingReply))) As Task(Of PingReply)
            ' Validate we're being used with a real smtpClient.  The rest of the arg validation 
            ' will happen in the call to sendAsync. 
            If ping Is Nothing Then
                Throw New ArgumentNullException("ping")
            End If

            ' Create a TaskCompletionSource to represent the operation 
            Dim tcs = New TaskCompletionSource(Of PingReply)(userToken)

            ' Register a handler that will transfer completion results to the TCS Task 
            Dim handler As PingCompletedEventHandler = Nothing
            handler = Function(sender, e) EAPCommon.HandleCompletion(tcs, e, Function() e.Reply, Function() ping.PingCompleted -= handler)
            AddHandler ping.PingCompleted, handler

            ' Try to start the async operation.  If starting it fails (due to parameter validation) 
            ' unregister the handler before allowing the exception to propagate. 
            Try
                sendAsync(tcs)
            Catch exc As Exception
                RemoveHandler ping.PingCompleted, handler
                tcs.TrySetException(exc)
            End Try

            ' Return the task to represent the asynchronous operation 
            Return tcs.Task
        End Function
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-08-04 06:44:58

第一个问题--将所有这些Function(tcs)位替换为Sub(tcs) --编译器是正确的,SendAsync不返回任何内容,而且您正在尝试提供一个Action,而不是Func

第二个问题--我们还没有EAPCommon.HandleCompletion的源代码,但我认为最后的论点需要改为类似于Sub() RemoveHandler ping.PingCompleted,handler的东西

内联Subs仅仅是用Visual 10介绍 (.NET 4/2010工具集),而您的转换器表示它现在支持.NET 3.5,所以这可能是它做了如此糟糕的工作的原因(尽管它生成的是无效的VB )。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6936547

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档