本文整理汇总了C#中System.Runtime.TimeoutHelper.ElapsedTime方法的典型用法代码示例。如果您正苦于以下问题:C# TimeoutHelper.ElapsedTime方法的具体用法?C# TimeoutHelper.ElapsedTime怎么用?C# TimeoutHelper.ElapsedTime使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Runtime.TimeoutHelper
的用法示例。
在下文中一共展示了TimeoutHelper.ElapsedTime方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Connect
public IConnection Connect(Uri uri, TimeSpan timeout)
{
if (DiagnosticUtility.ShouldTraceInformation)
{
TraceUtility.TraceEvent(TraceEventType.Information, 0x4002b, System.ServiceModel.SR.GetString("TraceCodeInitiatingTcpConnection"), new StringTraceRecord("Uri", uri.ToString()), this, null);
}
int port = uri.Port;
IPAddress[] iPAddresses = GetIPAddresses(uri);
Socket socket = null;
SocketException innerException = null;
if (port == -1)
{
port = 0x328;
}
int invalidAddressCount = 0;
TimeoutHelper helper = new TimeoutHelper(timeout);
for (int i = 0; i < iPAddresses.Length; i++)
{
if (helper.RemainingTime() == TimeSpan.Zero)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateTimeoutException(uri, helper.OriginalTimeout, iPAddresses, invalidAddressCount, innerException));
}
AddressFamily addressFamily = iPAddresses[i].AddressFamily;
if ((addressFamily == AddressFamily.InterNetworkV6) && !Socket.OSSupportsIPv6)
{
iPAddresses[i] = null;
}
else
{
DateTime utcNow = DateTime.UtcNow;
try
{
socket = new Socket(addressFamily, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(new IPEndPoint(iPAddresses[i], port));
innerException = null;
break;
}
catch (SocketException exception2)
{
invalidAddressCount++;
TraceConnectFailure(socket, exception2, uri, (TimeSpan) (DateTime.UtcNow - utcNow));
innerException = exception2;
socket.Close();
}
}
}
if (socket == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new EndpointNotFoundException(System.ServiceModel.SR.GetString("NoIPEndpointsFoundForHost", new object[] { uri.Host })));
}
if (innerException != null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(ConvertConnectException(innerException, uri, helper.ElapsedTime(), innerException));
}
return this.CreateConnection(socket);
}
示例2: ConnectAsync
public async Task<IConnection> ConnectAsync(Uri uri, TimeSpan timeout)
{
int port = uri.Port;
IPAddress[] addresses = await SocketConnectionInitiator.GetIPAddressesAsync(uri);
IConnection socketConnection = null;
SocketException lastException = null;
if (port == -1)
{
port = TcpUri.DefaultPort;
}
int invalidAddressCount = 0;
TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);
for (int i = 0; i < addresses.Length; i++)
{
if (timeoutHelper.RemainingTime() == TimeSpan.Zero)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
CreateTimeoutException(uri, timeoutHelper.OriginalTimeout, addresses, invalidAddressCount, lastException));
}
DateTime connectStartTime = DateTime.UtcNow;
try
{
socketConnection = await CreateConnectionAsync(addresses[i], port);
lastException = null;
break;
}
catch (SocketException socketException)
{
invalidAddressCount++;
lastException = socketException;
}
}
if (socketConnection == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
new EndpointNotFoundException(SR.Format(SR.NoIPEndpointsFoundForHost, uri.Host)));
}
if (lastException != null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
SocketConnectionInitiator.ConvertConnectException(lastException, uri,
timeoutHelper.ElapsedTime(), lastException));
}
return socketConnection;
}
示例3: Connect
public IConnection Connect(Uri uri, TimeSpan timeout)
{
if (DiagnosticUtility.ShouldTraceInformation)
{
TraceUtility.TraceEvent(TraceEventType.Information, TraceCode.InitiatingTcpConnection,
SR.GetString(SR.TraceCodeInitiatingTcpConnection),
new StringTraceRecord("Uri", uri.ToString()), this, null);
}
int port = uri.Port;
IPAddress[] addresses = SocketConnectionInitiator.GetIPAddresses(uri);
Socket socket = null;
SocketException lastException = null;
if (port == -1)
{
port = TcpUri.DefaultPort;
}
int invalidAddressCount = 0;
TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);
for (int i = 0; i < addresses.Length; i++)
{
if (timeoutHelper.RemainingTime() == TimeSpan.Zero)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
CreateTimeoutException(uri, timeoutHelper.OriginalTimeout, addresses, invalidAddressCount, lastException));
}
AddressFamily addressFamily = addresses[i].AddressFamily;
if (addressFamily == AddressFamily.InterNetworkV6 && !Socket.OSSupportsIPv6)
{
addresses[i] = null; // disregard for exception attempt purposes
continue;
}
DateTime connectStartTime = DateTime.UtcNow;
try
{
socket = new Socket(addressFamily, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(new IPEndPoint(addresses[i], port));
lastException = null;
break;
}
catch (SocketException socketException)
{
invalidAddressCount++;
SocketConnectionInitiator.TraceConnectFailure(socket, socketException, uri, DateTime.UtcNow - connectStartTime);
lastException = socketException;
socket.Close();
}
}
if (socket == null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
new EndpointNotFoundException(SR.GetString(SR.NoIPEndpointsFoundForHost, uri.Host)));
}
if (lastException != null)
{
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
SocketConnectionInitiator.ConvertConnectException(lastException, uri,
timeoutHelper.ElapsedTime(), lastException));
}
return CreateConnection(socket);
}