当前位置: 首页>>代码示例>>C#>>正文


C# IScheduler.ScheduleAsync方法代码示例

本文整理汇总了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();
                 }
             });
         });
     }
开发者ID:gilesbradshaw,项目名称:OpcClient,代码行数:21,代码来源:ConsoleObservable.cs

示例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);
		}
开发者ID:HackedBrain,项目名称:HackedBrain.Azure,代码行数:92,代码来源:MessageClientEntityHelpers.cs

示例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);
		}
开发者ID:HackedBrain,项目名称:HackedBrain.Azure,代码行数:80,代码来源:MessageReceiverExtensions.cs


注:本文中的IScheduler.ScheduleAsync方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。