本文整理汇总了C#中System.Threading.CancellationToken.CreateCancelTask方法的典型用法代码示例。如果您正苦于以下问题:C# CancellationToken.CreateCancelTask方法的具体用法?C# CancellationToken.CreateCancelTask怎么用?C# CancellationToken.CreateCancelTask使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Threading.CancellationToken
的用法示例。
在下文中一共展示了CancellationToken.CreateCancelTask方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DoConnect
protected override async Task<SourceConnectionClient> DoConnect(Uri source, CancellationToken cancellationToken)
{
TcpClient client = null;
var bind_addr = GetBindAddresses(source);
if (bind_addr.Count()==0) {
this.state = ConnectionState.Error;
throw new BindErrorException(String.Format("Cannot resolve bind address: {0}", source.DnsSafeHost));
}
var listeners = bind_addr.Select(addr => new TcpListener(addr)).ToArray();
try {
var cancel_task = cancellationToken.CreateCancelTask<TcpClient>();
var tasks = listeners.Select(listener => {
listener.Start(1);
Logger.Debug("Listening on {0}", listener.LocalEndpoint);
return listener.AcceptTcpClientAsync();
}).Concat(Enumerable.Repeat(cancel_task, 1)).ToArray();
var result = await Task.WhenAny(tasks);
if (!result.IsCanceled) {
client = result.Result;
Logger.Debug("Client accepted");
}
else {
Logger.Debug("Listen cancelled");
}
}
catch (SocketException) {
this.state = ConnectionState.Error;
throw new BindErrorException(String.Format("Cannot bind address: {0}", bind_addr));
}
finally {
foreach (var listener in listeners) {
listener.Stop();
}
}
if (client!=null) {
return new SourceConnectionClient(client);
}
else {
return null;
}
}