本文整理汇总了C#中System.Threading.CancellationToken.WithTimeout方法的典型用法代码示例。如果您正苦于以下问题:C# CancellationToken.WithTimeout方法的具体用法?C# CancellationToken.WithTimeout怎么用?C# CancellationToken.WithTimeout使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Threading.CancellationToken
的用法示例。
在下文中一共展示了CancellationToken.WithTimeout方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: WaitForAsync
private static async Task<bool> WaitForAsync(
[NotNull] ServiceController serviceController,
ServiceControllerStatus status,
CancellationToken token = default(CancellationToken))
{
if (serviceController == null) throw new ArgumentNullException("serviceController");
// TODO Remove constant timeout
using (ITokenSource tokenSource = token.WithTimeout(TimeSpan.FromMinutes(1)))
{
token = tokenSource.Token;
serviceController.Refresh();
while (serviceController.Status != status)
{
// ReSharper disable once PossibleNullReferenceException
await Task.Delay(250, token).ConfigureAwait(false);
if (token.IsCancellationRequested) return false;
serviceController.Refresh();
}
}
return true;
}
示例2: RunAsync
public static async Task RunAsync(
[NotNull] string description,
[CanBeNull] NamedPipeServerInfo service = null,
CancellationToken token = default(CancellationToken))
{
if (description == null) throw new ArgumentNullException("description");
if (!ConsoleHelper.IsConsole)
return;
try
{
Log.SetTrace(validLevels: LoggingLevels.None);
Log.SetConsole(Log.ShortFormat);
Console.Title = description;
NamedPipeClient client = null;
while (client == null)
{
token.ThrowIfCancellationRequested();
while (service == null ||
!service.IsValid)
{
Console.Clear();
NamedPipeServerInfo[] services = null;
await Log.Flush(token).ConfigureAwait(false);
// ReSharper disable once AssignNullToNotNullAttribute
ConsoleTextWriter.Default.WriteLine(ClientResources.ConsoleClient_RunAsync_ScanningForService);
while (services == null ||
services.Length < 1)
{
services = NamedPipeClient.GetServices().ToArray();
if (Console.KeyAvailable)
return;
// ReSharper disable once PossibleNullReferenceException
await Task.Delay(500, token).ConfigureAwait(false);
token.ThrowIfCancellationRequested();
}
if (services.Length > 0)
WriteServerList(services);
// ReSharper disable once AssignNullToNotNullAttribute
ConsoleTextWriter.Default.WriteLine(ClientResources.ConsoleClient_RunAsync_EnterServiceName);
string serviceName = Console.ReadLine();
service = !string.IsNullOrWhiteSpace(serviceName)
? NamedPipeClient.FindService(serviceName)
: NamedPipeClient.GetServices().FirstOrDefault();
}
Console.Clear();
// ReSharper disable once AssignNullToNotNullAttribute
ConsoleTextWriter.Default.WriteLine(
ClientResources.ConsoleClient_RunAsync_ConnectingToService,
service.Name);
try
{
// TODO Remove constant timeout
using (ITokenSource tokenSource = token.WithTimeout(10000))
client =
await
NamedPipeClient.Connect(
description,
service,
OnReceive,
tokenSource.Token)
.ConfigureAwait(false);
}
catch (TaskCanceledException)
{
// ReSharper disable once AssignNullToNotNullAttribute
ConsoleTextWriter.Default.WriteLine(
ClientResources.ConsoleClient_RunAsync_TimedOut,
service.Name);
// ReSharper disable once AssignNullToNotNullAttribute
ConsoleTextWriter.Default.WriteLine(ClientResources.ConsoleClient_RunAsync_PressAnyKeyContinue);
client = null;
service = null;
Console.ReadKey(true);
}
}
// ReSharper disable once AssignNullToNotNullAttribute
Console.Title = string.Format(
ClientResources.ConsoleClient_RunAsync_ConnectedTitle,
description,
service.Name);
_connected.WriteToConsole(
null,
new Dictionary<string, object>
{
{ "ServiceName", client.ServiceName }
});
// ReSharper disable once PossibleNullReferenceException
await Task.Delay(1100, token).ConfigureAwait(false);
await Log.Flush(token).ConfigureAwait(false);
//.........这里部分代码省略.........