本文整理汇总了C#中IScheduler.ScheduleAsync方法的典型用法代码示例。如果您正苦于以下问题:C# IScheduler.ScheduleAsync方法的具体用法?C# IScheduler.ScheduleAsync怎么用?C# IScheduler.ScheduleAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IScheduler
的用法示例。
在下文中一共展示了IScheduler.ScheduleAsync方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ConsoleInputObservable
public static IObservable<string> ConsoleInputObservable(
IScheduler scheduler = null)
{
scheduler = scheduler ?? Scheduler.Default;
return Observable.Create<string>(o =>
{
return scheduler.ScheduleAsync(async (ctrl, ct) =>
{
while (!ct.IsCancellationRequested)
{
System.Diagnostics.Debug.WriteLine($"reading line {DateTime.Now.ToLongTimeString()}");
var next = Console.ReadLine();
if (ct.IsCancellationRequested)
return;
using(new Timer($"readLine -- {next} "))
o.OnNext(next);
await ctrl.Yield();
}
});
});
}
示例2: WhenSessionAccepted
public static IObservable<MessageSession> WhenSessionAccepted(this MessageClientEntity messageClientEntity, Func<Task<MessageSession>> acceptMessageSessionImpl, IScheduler scheduler)
{
#if DEBUG
if(acceptMessageSessionImpl == null)
{
throw new ArgumentNullException("acceptMessageSessionImpl");
}
if(messageClientEntity == null)
{
throw new ArgumentNullException("messageClientEntity");
}
if(scheduler == null)
{
throw new ArgumentNullException("scheduler");
}
#endif
IObservable<MessageSession> brokeredMessages = Observable.Create<MessageSession>(
observer =>
{
Func<IScheduler, CancellationToken, Task<IDisposable>> messageSessionPump = null;
messageSessionPump = async (previousScheduler, cancellationToken) =>
{
IDisposable disposable;
if(!cancellationToken.IsCancellationRequested)
{
if(messageClientEntity.IsClosed)
{
observer.OnCompleted();
disposable = Disposable.Empty;
}
else
{
try
{
MessageSession nextMessageSession = await acceptMessageSessionImpl();
if(nextMessageSession != null)
{
observer.OnNext(nextMessageSession);
disposable = ImmediateScheduler.Instance.ScheduleAsync(messageSessionPump);
}
else
{
observer.OnCompleted();
disposable = Disposable.Empty;
}
}
catch(OperationCanceledException)
{
observer.OnCompleted();
disposable = Disposable.Empty;
}
catch(TimeoutException)
{
observer.OnCompleted();
disposable = Disposable.Empty;
}
catch(Exception exception)
{
observer.OnError(exception);
disposable = Disposable.Empty;
}
}
}
else
{
observer.OnCompleted();
disposable = Disposable.Empty;
}
return disposable;
};
return scheduler.ScheduleAsync(messageSessionPump);
});
return Observable.Using(() => Disposable.Create(messageClientEntity.Close), _ => brokeredMessages);
}
示例3: WhenMessageReceived
public static IObservable<BrokeredMessage> WhenMessageReceived(this MessageReceiver messageReceiver, TimeSpan receiveWaitTime, IScheduler scheduler)
{
if(messageReceiver == null)
{
throw new ArgumentNullException("messageReceiver");
}
IObservable<BrokeredMessage> brokeredMessages = Observable.Create<BrokeredMessage>(
observer =>
{
Func<Task<BrokeredMessage>> receiveFunc = receiveWaitTime == TimeSpan.MinValue ? messageReceiver.ReceiveAsync : new Func<Task<BrokeredMessage>>(() => messageReceiver.ReceiveAsync(receiveWaitTime));
Func<IScheduler, CancellationToken, Task<IDisposable>> messagePump = null;
messagePump = async (previousScheduler, cancellationToken) =>
{
IDisposable disposable;
if(!cancellationToken.IsCancellationRequested)
{
if(messageReceiver.IsClosed)
{
observer.OnCompleted();
disposable = Disposable.Empty;
}
else
{
try
{
BrokeredMessage nextMessage = await receiveFunc();
if(nextMessage != null)
{
observer.OnNext(nextMessage);
disposable = ImmediateScheduler.Instance.ScheduleAsync(messagePump);
}
else
{
observer.OnCompleted();
disposable = Disposable.Empty;
}
}
catch(OperationCanceledException)
{
observer.OnCompleted();
disposable = Disposable.Empty;
}
catch(TimeoutException)
{
observer.OnCompleted();
disposable = Disposable.Empty;
}
catch(Exception exception)
{
observer.OnError(exception);
disposable = Disposable.Empty;
}
}
}
else
{
observer.OnCompleted();
disposable = Disposable.Empty;
}
return disposable;
};
return scheduler.ScheduleAsync(messagePump);
});
return Observable.Using(() => Disposable.Create(() => messageReceiver.Close()), _ => brokeredMessages);
}