【发布时间】:2011-08-04 14:55:39
【问题描述】:
您好,我已将此并行扩展 c# 代码示例转换为 VB.NET
使用 Developerfusion 工具 here,但我遇到了多个错误,但由于我有限的 C# 经验无法解决。
1) 出现错误后,我将 System.Runtime.CompilerServices.Extension 转换为 Global.System.Runtime.CompilerServices.ExtensionAttribute,这是我能想到的最接近的值,但在线上出现错误 (26)
Return SendTaskCore(ping, userToken, Function(tcs) ping.SendAsync(address, tcs))
说ping.SendAsync(address, timeout, tcs) 不会产生值
2) 196 号线附近
handler = Function(sender, e) EAPCommon.HandleCompletion(tcs, e, Function() e.Reply, Function() ping.PingCompleted -= handler)
“ping.PingCompleted”提示错误
'公共事件PingCompleted(sender As Object, e As System.Net.NetworkInformation.PingCompletedEventArgs)' 是一个事件, 并且不能直接调用。使用“RaiseEvent”语句来提高 一个事件。
任何建议将不胜感激。完整代码如下(注释已删除),原文出处
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 代码:
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
【问题讨论】:
-
你能发布你的VB.NET代码吗?
-
谢谢蒂姆,它现在在上面。