本文整理汇总了C#中IObserver.OnNext方法的典型用法代码示例。如果您正苦于以下问题:C# IObserver.OnNext方法的具体用法?C# IObserver.OnNext怎么用?C# IObserver.OnNext使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IObserver
的用法示例。
在下文中一共展示了IObserver.OnNext方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: MainAsync
public static async Task MainAsync(IObserver<string> observer)
{
observer.OnNext("start");
await I.Task.Delay(delay);
observer.OnNext("1");
await Observable.Timer(delay);
observer.OnNext("2");
await Task.Delay(delay);
observer.OnNext("3");
await I.Task.Delay(delay);
var urls = new[]
{
"http://yahoo.co.jp",
"http://google.co.jp",
"http://bing.co.jp",
"http://awsedrftgyhujikol.jp/",
};
foreach (var url in urls)
{
try
{
var res = await GetAsStringAsync(url);
observer.OnNext(res);
}
catch (Exception ex)
{
observer.OnError(ex);
}
}
observer.OnCompleted();
}
示例2: Subscribe
public static IDisposable Subscribe(IObserver<int> observer)
{
observer.OnNext(0);
observer.OnNext(1);
observer.OnNext(2);
observer.OnCompleted();
return Disposable.Empty;
}
示例3: PostScheduled
public Task PostScheduled(CommandEnvelope envelope, IObserver<LogEntry> log)
{
var req = CreatePostCommandRequest(envelope);
return _client.PostScheduled(req)
.ContinueWith(x =>
{
var resp = x.Result;
var id = resp.Id;
Action<string> traceStatus = m => log.OnNext(new LogEntry
{
Message = m,
Severity = LogEntrySeverity.Trace,
Timestamp = DateTime.Now
});
traceStatus("Start waiting for execution command " + id);
var token = DateTime.MinValue;
while (true)
{
var resp2 = _client.GetScheduled(id, token);
// Write user log
var serverLog = resp2.Log.OrEmpty().ToList();
foreach (var entry in serverLog)
{
log.OnNext(entry);
}
// Take maximum available timestamp
token = serverLog.Select(xx => xx.Timestamp).Union(new[] { token }).Max();
switch (resp2.Status)
{
case ScheduledCommandStatus.Pending:
traceStatus("Command(" + id + ") is still pending.");
break;
case ScheduledCommandStatus.InProgress:
traceStatus("Command(" + id + ") is in progress.");
break;
case ScheduledCommandStatus.Success:
traceStatus("Command(" + id + ") completed success.");
return;
case ScheduledCommandStatus.Failure:
traceStatus("Command(" + id + ") failed.");
return;
}
Thread.Sleep(300);
}
});
}
示例4: DownloadAudioAsync
public static async Task DownloadAudioAsync(VideoInfo videoInfo, string downloadPath, IObserver<double> progress)
{
string cleanedTitle = RemoveIllegalPathCharacters(videoInfo.Title);
var downloader = new AudioDownloader(videoInfo, Path.Combine(downloadPath, cleanedTitle + videoInfo.AudioExtension));
downloader.DownloadProgressChanged += (sender, args) => progress.OnNext(args.ProgressPercentage * 0.95);
downloader.AudioExtractionProgressChanged += (sender, args) => progress.OnNext(95 + args.ProgressPercentage * 0.05);
await DownloadFromYoutube(downloader, new[] { typeof(IOException), typeof(WebException), typeof(AudioExtractionException) }, progress);
}
示例5: ToIterate_Complete
IEnumerable<IObservable<Object>> ToIterate_Complete(IObservable<int> xs, IObservable<int> ys, IObservable<int> zs, IObserver<int> observer)
{
observer.OnNext(1);
yield return xs.Select(x => new Object());
observer.OnNext(2);
yield return ys.Select(x => new Object());
observer.OnNext(3);
observer.OnCompleted();
yield return zs.Select(x => new Object());
observer.OnNext(4);
}
示例6: Clone
/// <inheritdoc />
public static IObservable<IObservableRepository> Clone(
string sourceUrl,
string workingDirectory,
IObserver<Tuple<string, int>> observer,
CredentialsHandler credentials = null)
{
var isCancelled = false;
var options = new CloneOptions
{
Checkout = true,
CredentialsProvider = credentials,
OnTransferProgress = progress =>
{
// TODO: how should we signal for the "indexing objects" events
var p = (100 * progress.ReceivedObjects) / progress.TotalObjects;
var receivingMessage = String.Format("Receiving objects: {0}% ({1}/{2})", p, progress.ReceivedObjects, progress.TotalObjects);
observer.OnNext(Tuple.Create(receivingMessage, p));
return !isCancelled;
},
IsBare = false,
OnCheckoutProgress = ProgressFactory.CreateHandlerWithoutMessages(observer)
};
var directoryInfo = new DirectoryInfo(workingDirectory);
var initialMessage = String.Format("Cloning into '{0}'...", directoryInfo.Name);
observer.OnNext(Tuple.Create(initialMessage, 0));
return Observable.Create<ObservableRepository>(subj =>
{
var sub = Observable.Start(() =>
{
var directory = Repository.Clone(sourceUrl, workingDirectory, options);
observer.OnNext(Tuple.Create("clone completed", 100));
observer.OnCompleted();
return new ObservableRepository(directory, credentials);
}, Scheduler.Default).Subscribe(subj);
return new CompositeDisposable(
sub,
Disposable.Create(() =>
{
isCancelled = true;
observer.OnCompleted();
}));
});
}
示例7: Accept
bool Accept(IObserver<Interactor> observer)
{
Log.Info("Accept new connection");
try
{
var newSocket = _listener.Accept();
var interactor = new Interactor(newSocket, _nextInteractorId++, _bufferManager);
_interactors.Add(newSocket, interactor);
Log.DebugFormat("Accepted {0}", interactor);
observer.OnNext(interactor);
return true;
}
catch (SocketException ex)
{
if (ex.SocketErrorCode == SocketError.Interrupted)
observer.OnCompleted();
else
observer.OnError(ex);
}
catch (Exception ex)
{
observer.OnError(ex);
}
return false;
}
示例8: FetchBytes
static IEnumerator FetchBytes(WWW www, IObserver<byte[]> observer, IProgress<float> reportProgress, CancellationToken cancel)
{
using (www)
{
while (!www.isDone && !cancel.IsCancellationRequested)
{
if (reportProgress != null)
{
try
{
reportProgress.Report(www.progress);
}
catch (Exception ex)
{
observer.OnError(ex);
yield break;
}
}
yield return null;
}
if (cancel.IsCancellationRequested) yield break;
if (!string.IsNullOrEmpty(www.error))
{
observer.OnError(new Exception(www.error));
}
else
{
observer.OnNext(www.bytes);
observer.OnCompleted();
}
}
}
示例9: ScheduledMethod
private static IDisposable ScheduledMethod(IObserver<int> o)
{
var cancel = new CancellationDisposable();
// internally creates a new CancellationTokenSource
NewThreadScheduler.Default.Schedule(() =>
{
int i = 0;
for (;;)
{
Thread.Sleep(200);
// here we do the long lasting background operation
if (!cancel.Token.IsCancellationRequested)
// check cancel token periodically
o.OnNext(i++);
else
{
Console.WriteLine(
"Aborting because cancel event was signaled!");
o.OnCompleted();
return;
}
}
}
);
return cancel;
}
示例10: RunAsync
public async Task RunAsync(IObserver<string> observer)
{
var urls = new[]
{
"http://yahoo.co.jp",
"http://google.co.jp",
"http://bing.co.jp",
"http://awsedrftgyhujikol.jp/",
};
foreach (var url in urls)
{
await Observable.Timer(TimeSpan.FromSeconds(1));
try
{
var res = await GetAsStringAsync(url);
observer.OnNext(res);
}
catch(Exception ex)
{
observer.OnError(ex);
}
}
}
示例11: Pull
/// <inheritdoc />
public IObservable<MergeResult> Pull(
IObserver<Tuple<string, int>> observer)
{
var signature = _repository.Config.BuildSignature(DateTimeOffset.Now);
var isCancelled = false;
var options = new PullOptions
{
FetchOptions = new FetchOptions
{
TagFetchMode = TagFetchMode.None,
CredentialsProvider = _credentialsHandler,
OnTransferProgress = progress =>
{
// TODO: how should we signal for the "indexing objects" events
var p = (50 * progress.ReceivedObjects) / progress.TotalObjects;
observer.OnNext(Tuple.Create("", p));
return !isCancelled;
}
},
MergeOptions = new MergeOptions
{
OnCheckoutProgress = ProgressFactory.CreateHandler(observer, start:50, count:50)
}
};
return Observable.Create<MergeResult>(subj =>
{
var sub = Observable.Start(() =>
{
var result = _repository.Network.Pull(signature, options);
observer.OnNext(Tuple.Create("pull completed", 100));
observer.OnCompleted();
return result;
}, Scheduler.Default).Subscribe(subj);
return new CompositeDisposable(
sub,
Disposable.Create(() =>
{
isCancelled = true;
observer.OnCompleted();
}));
});
}
示例12: DownloadUrl
public IObservable<string> DownloadUrl(string url, IObserver<int> progress = null)
{
progress = progress ?? new Subject<int>();
progress.OnNext(100);
progress.OnCompleted();
return Observable.Return("");
}
示例13: QueueBackgroundDownloads
public IObservable<Unit> QueueBackgroundDownloads(IEnumerable<string> urls, IEnumerable<string> localPaths, IObserver<int> progress = null)
{
progress = progress ?? new Subject<int>();
progress.OnNext(100);
progress.OnCompleted();
return Observable.Return(Unit.Default);
}
示例14: EventSourceLoop
// イベントを飛ばし続けるループ
static async Task EventSourceLoop(IObserver<int> observer, CancellationToken ct)
{
for (var i = 0; !ct.IsCancellationRequested; ++i)
{
observer.OnNext(i);
await Task.Delay(Interval);
}
}
示例15: CreateHandlerWithoutMessages
public static CheckoutProgressHandler CreateHandlerWithoutMessages(
IObserver<Tuple<string, int>> observer,
int start = 0,
int count = 100)
{
return (path, completedSteps, totalSteps) =>
{
if (totalSteps == 0)
{
observer.OnNext(Tuple.Create(path, 0));
}
else
{
var progress = start + (count * completedSteps) / totalSteps;
observer.OnNext(Tuple.Create("", progress));
}
};
}