Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6229243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:32:02+00:00 2026-05-24T09:32:02+00:00

Hi I have converted this parallel extension c# code sample to VB.NET http://code.msdn.microsoft.com/Samples-for-Parallel-b4b76364/sourcecode?fileId=25353&pathId=215900242 using

  • 0

Hi I have converted this parallel extension c# code sample to VB.NET

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

using the Developerfusion tool here but I am getting multiple errors that I cannot resolve with my limited C# experience.

1) After getting errors I converted System.Runtime.CompilerServices.Extension to Global.System.Runtime.CompilerServices.ExtensionAttribute which is the closest I could come up with, and I get errors on the line (26)

Return SendTaskCore(ping, userToken, Function(tcs) ping.SendAsync(address, tcs))

saying that ping.SendAsync(address, timeout, tcs) does not produce a value

2) Around line 196

handler = Function(sender, e) EAPCommon.HandleCompletion(tcs, e, Function() e.Reply, Function() ping.PingCompleted -= handler)

I get an error on ‘ping.PingCompleted’ saying

‘Public Event PingCompleted(sender As Object, e As
System.Net.NetworkInformation.PingCompletedEventArgs)’ is an event,
and cannot be called directly. Use a ‘RaiseEvent’ statement to raise
an event.

Any suggestions would be appreciated. The full code follows (Comments removed), original source

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

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; 
        } 
    } 
} 

EDIT: Here is the converted VB code:

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T09:32:02+00:00Added an answer on May 24, 2026 at 9:32 am

    First problem – replace all of those Function(tcs) bits with Sub(tcs) – the compiler is correct, SendAsync doesn’t return anything, and anyway, you’re trying to supply an Action, not a Func.


    Second problem – We don’t yet have source for EAPCommon.HandleCompletion, but I think the final argument needs to be changed to something like Sub() RemoveHandler ping.PingCompleted,handler


    Inline Subs were only introduced with Visual Basic 10 (.NET 4/2010 toolset), whereas your converter says that it now supports .NET 3.5, so that’s probably why it did such a bad job (although what it was producing wasn’t valid VB anyway)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have converted a textbox search to Google Search using : location.href = http://images.google.com/search?q=
I wrote this code I have these errors Cannot implicitly convert type x.Program.TreeNode' to
We have a Microsoft SQL Server 2005 database that needs to be converted back
I have these code which I have converted from Java. Now I need to
I have a problem to upgrade my routes.rb file. I converted this route: map.resource
We have converted our site from HTTP to HTTPS, and when our login forms
I have converted text to Image(.png) using ASP.NET C# in Visual Studio 2010. But
I have this HTML structure and want to convert it to an accordion. <div
I have this method in my db class public function query($queryString) { if (!$this->_connected)
I am working with a set of data that I have converted to a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.