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 =开发者_StackOverflow中文版> 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
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)
精彩评论