本文整理汇总了C#中Subject.OnCompleted方法的典型用法代码示例。如果您正苦于以下问题:C# Subject.OnCompleted方法的具体用法?C# Subject.OnCompleted怎么用?C# Subject.OnCompleted使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Subject
的用法示例。
在下文中一共展示了Subject.OnCompleted方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Start
private void Start()
{
subject = new Subject<int>();
/*
以下は、SubjectをGUIでいろいろいじれるようにしたものである。
いくつか実行のパターンを記すので、この順序でボタンを押してどうなるか確認してほしい
1.Subscribe → OnNext → OnCompleted (値が発行されたあとストリームが終了する)
2.OnNext → Subscribe → OnNext → OnComplated (Subscribeしたタイミングでストリームが生成され値が受け取れるようになる)
3.Subscribe ×2 → OnNext → OnCompleted (Subscribeを実行した数だけストリームが生成され稼働する)
*/
// Subscribeボタンが押されたらSubjectをSubscribeしてresultTextに表示する
subscribeButton.OnClickAsObservable().Subscribe(_ =>
{
if (subject != null)
{
subject.Subscribe(
time => resultText.text += time.ToString() + " ", //OnNext
() => resultText.text += "OnCompleted "); //OnCompleted
}
});
// OnNextボタンが押されたら今が何度目のOnNextであるかを発行する
onNextButton.OnClickAsObservable().Subscribe(_ =>
{
if (subject != null)
{
subject.OnNext(++onNextCount);
}
});
// OnCompletedボタンが押されたらOnCompletedを発行する
onCompletedButton.OnClickAsObservable().Subscribe(_ =>
{
if (subject != null)
{
subject.OnCompleted();
}
});
// Resetボタンが押されたら全体を初期化する
resetButton.OnClickAsObservable().Subscribe(_ =>
{
if (subject != null)
{
subject.OnCompleted();
}
subject = new Subject<int>();
resultText.text = "";
onNextCount = 0;
});
}
示例2: StartsTrueBecomesFalseWhenCompleted
public void StartsTrueBecomesFalseWhenCompleted()
{
var repoSubject = new Subject<IRepositoryModel[]>();
var repositoryHost = Substitute.For<IRepositoryHost>();
repositoryHost.ModelService.GetRepositories().Returns(repoSubject);
var cloneService = Substitute.For<IRepositoryCloneService>();
var vm = new RepositoryCloneViewModel(
repositoryHost,
cloneService,
Substitute.For<IOperatingSystem>(),
Substitute.For<IVSServices>());
Assert.False(vm.IsLoading);
vm.LoadRepositoriesCommand.ExecuteAsync().Subscribe();
Assert.True(vm.IsLoading);
repoSubject.OnNext(new[] { Substitute.For<IRepositoryModel>() });
repoSubject.OnNext(new[] { Substitute.For<IRepositoryModel>() });
Assert.True(vm.IsLoading);
repoSubject.OnCompleted();
Assert.False(vm.IsLoading);
}
示例3: DoALongRunningOperation
public void DoALongRunningOperation(string id)
{
var subject = new Subject<string>();
subject.ToClientside().Observable<RxHub>(id);
var debugObserver = subject.Subscribe(
s => Debug.Print("OnNext: \"{0}\"", s),
ex => Debug.Print("OnError: \"{0}\"", ex.Message),
() => Debug.WriteLine("OnCompleted: we are done!")
);
Task.Run(() =>
{
Debug.WriteLine("Publishing \"just started\"...");
subject.OnNext("just started");
Thread.Sleep(1000);
Debug.WriteLine("Publishing \"One second passed, I'm still running\"...");
subject.OnNext("One second passed, I'm still running");
Thread.Sleep(5000);
Debug.WriteLine("Publishing \"Another five seconds passed, I'm still running\"...");
subject.OnNext("Another five seconds passed, I'm still running");
Thread.Sleep(5000);
Debug.WriteLine("Publishing \"Almost done\"...");
subject.OnNext("Almost done");
Debug.WriteLine("Completing...");
subject.OnCompleted();
Debug.WriteLine("Completed.");
});
}
示例4: last_value_is_received_after_completed_if_any_connection_is_disposed
public void last_value_is_received_after_completed_if_any_connection_is_disposed()
{
Subject<int> subject = new Subject<int>();
var stats = new StatsObserver<int>();
var obs = subject.Prune();
var connectionA = obs.Connect();
var connectionB = obs.Connect();
subject.OnNext(1);
subject.OnNext(2);
subject.OnNext(3);
connectionA.Dispose();
subject.OnCompleted();
connectionB.Dispose();
obs.Subscribe(stats);
Assert.IsFalse(stats.NextCalled);
Assert.IsFalse(stats.CompletedCalled);
}
示例5: CorrectlyShutsDown
public void CorrectlyShutsDown()
{
var fixture = new KeyedOperationQueue();
var op1 = new Subject<int>();
var op2 = new Subject<int>();
var op3 = new Subject<int>();
bool isCompleted = false;
int op1Result = 0, op2Result = 0, op3Result = 0;
fixture.EnqueueObservableOperation("foo", () => op1).Subscribe(x => op1Result = x);
fixture.EnqueueObservableOperation("bar", () => op2).Subscribe(x => op2Result = x);
// Shut down the queue, shouldn't be completed until op1 and op2 complete
fixture.ShutdownQueue().Subscribe(_ => isCompleted = true);
Assert.False(isCompleted);
op1.OnNext(1); op1.OnCompleted();
Assert.False(isCompleted);
Assert.Equal(1, op1Result);
op2.OnNext(2); op2.OnCompleted();
Assert.True(isCompleted);
Assert.Equal(2, op2Result);
// We've already shut down, new ops should be ignored
fixture.EnqueueObservableOperation("foo", () => op3).Subscribe(x => op3Result = x);
op3.OnNext(3); op3.OnCompleted();
Assert.Equal(0, op3Result);
}
示例6: CombineLatest
public void CombineLatest()
{
var a = new Subject<int>();
var b = new Subject<int>();
a.OnNext(10);
b.OnNext(20);
var l = Enumerable.Empty<Unit>().Select(_ => Notification.CreateOnNext(new { x = 0, y = 0 })).ToList();
a.CombineLatest(b, (x, y) => new { x, y }).Materialize().Subscribe(x => l.Add(x));
a.OnNext(1000);
b.OnNext(2000);
l[0].Value.Is(new { x = 1000, y = 2000 });
b.OnNext(3000);
l[1].Value.Is(new { x = 1000, y = 3000 });
a.OnNext(5000);
l[2].Value.Is(new { x = 5000, y = 3000 });
a.OnCompleted();
l.Count.Is(3);
a.OnNext(1001);
l.Count.Is(3);
b.OnNext(5);
l[3].Value.Is(new { x = 5000, y = 5 });
b.OnNext(500);
l[4].Value.Is(new { x = 5000, y = 500 });
b.OnCompleted();
l[5].Kind.Is(NotificationKind.OnCompleted);
}
示例7: Main
protected override void Main()
{
TraceLine(Instructions.PressAnyKeyToCancel);
var queries = new Subject<IObservable<string>>();
var zipped = queries
.Zip()
.Select(list => list.Aggregate(string.Empty, (acc, cur) => acc += cur + " "))
.Take(3);
using (zipped.Subscribe(ConsoleOutput))
{
var xs = Observable.Interval(TimeSpan.FromSeconds(.5));
var ys = Observable.Interval(TimeSpan.FromSeconds(1));
var zs = Observable.Interval(TimeSpan.FromSeconds(1.5));
queries.OnNext(xs.Select(value => Text.First + ' ' + value));
queries.OnNext(ys.Select(value => Text.Second + ' ' + value));
queries.OnNext(zs.Select(value => Text.Third + ' ' + value));
queries.OnCompleted();
WaitForKey();
}
}
示例8: Zip
public void Zip()
{
var a = new Subject<int>();
var b = new Subject<int>();
a.OnNext(10);
b.OnNext(20);
var l = Enumerable.Empty<Unit>().Select(_ => Notification.CreateOnNext(new { x = 0, y = 0 })).ToList();
a.Zip(b, (x, y) => new { x, y }).Materialize().Subscribe(x => l.Add(x));
a.OnNext(1000);
b.OnNext(2000);
a.OnCompleted();
l.Count.Is(1); // OnNext
a.OnNext(1001);
l.Count.Is(1);
b.OnNext(5);
l.Count.Is(2); // Completed!
l[1].Kind.Is(NotificationKind.OnCompleted);
}
示例9: Run
public static async Task Run()
{
var s = new Subject<TestDisposable>();
var obs = s.AsObservable().AddRef();
// obs.Where(d => d.Id % 2 == 0).Subscribe(d => d.Write("Where Even"));
// obs.Where(d => d.Id % 2 == 1).Subscribe(d => d.Write("Where Odd"));
// obs.Skip(1).Take(2).Subscribe(d => d.Write("Skip Take"));
await Program.SwitchToCompute();
obs.Using().Skip(2).Take(2).ObserveOn(Program.Monitor).Subscribe(d =>
{
// using (d)
{
d.Write("Challenging Pre...");
// await Program.SwitchToCompute();
d.Write("Challenging Middle...");
// await Program.SwitchToRender();
d.Write("Challenging Post...");
}
}, () => Console.WriteLine("Challenging Completed"));
for (int i = 0; i < 8; ++i)
using (var d = new TestDisposable())
s.OnNext(d);
s.OnCompleted();
await TaskEx.Delay(30000);
if (Global.Yes)
return;
}
示例10: UseObserverTest1
public void UseObserverTest1()
{
var subject = new Subject<string>();
var result = new List<Tuple<int, string>>();
subject
.Select(x => new Tuple<int, string>(0, x))
.UseObserver((observer, value, i) =>
{
observer.OnNext(new Tuple<int, string>(i, value.Item2));
},
null,
observer =>
{
observer.OnNext(new Tuple<int, string>(-1, "end"));
})
.Subscribe(result.Add);
subject.OnNext("a");
subject.OnNext("b");
subject.OnNext("c");
subject.OnCompleted();
result.Is(
new Tuple<int, string>(0, "a"),
new Tuple<int, string>(1, "b"),
new Tuple<int, string>(2, "c"),
new Tuple<int, string>(-1, "end"));
}
示例11: DeterministicDisposal
private static void DeterministicDisposal()
{
Demo.DisplayHeader("The Using operator will make sure that the resource is disposed no matter what caused the observable to stop");
Subject<int> subject = new Subject<int>();
var observable =
Observable.Using(() => Disposable.Create(() => { Console.WriteLine("DISPOSED"); }),
_ => subject);
Console.WriteLine();
Console.WriteLine("Disposed when completed");
observable.SubscribeConsole();
subject.OnCompleted();
Console.WriteLine();
Console.WriteLine("Disposed when error occurs");
subject = new Subject<int>();
observable.SubscribeConsole();
subject.OnError(new Exception("error"));
Console.WriteLine();
Console.WriteLine("Disposed when subscription disposed");
subject = new Subject<int>();
var subscription =
observable.SubscribeConsole();
subscription.Dispose();
}
示例12: DistinctUntilChanged
public void DistinctUntilChanged()
{
{
var subject = new Subject<int>();
int[] array = null;
var disp = subject.DistinctUntilChanged().ToArray().Subscribe(xs => array = xs);
Array.ForEach(new[] { 1, 10, 10, 1, 100, 100, 100, 5 }, subject.OnNext);
subject.OnCompleted();
array.Is(1, 10, 1, 100, 5);
}
{
var subject = new Subject<int>();
int[] array = null;
var disp = subject.DistinctUntilChanged(x => x, EqualityComparer<int>.Default).ToArray().Subscribe(xs => array = xs);
Array.ForEach(new[] { 1, 10, 10, 1, 100, 100, 100, 5 }, subject.OnNext);
subject.OnCompleted();
array.Is(1, 10, 1, 100, 5);
}
}
示例13: ParserSelectManyCollection_MovesToEndOfFirstMatch
public void ParserSelectManyCollection_MovesToEndOfFirstMatch()
{
using (var parserCompleted = new ManualResetEventSlim())
using (var source = new Subject<int>())
{
var results = source
.ObserveOn(ThreadPoolScheduler.Instance)
.Parse(parser =>
from next in parser
select from elements in next.Exactly(2)
from value in elements
where value < 2
select value);
var values = new List<int>();
using (results.Subscribe(values.Add, parserCompleted.Set))
{
source.OnNext(2);
source.OnNext(2);
source.OnNext(0);
source.OnNext(1);
source.OnNext(0);
source.OnNext(1);
source.OnCompleted();
parserCompleted.Wait();
Assert.AreEqual(4, values.Count);
}
}
}
示例14: Should_Complete_When_Update_Observable_Completes
public void Should_Complete_When_Update_Observable_Completes()
{
var update = new Subject<Unit>();
var target = new ExpressionObserver(() => 1, "Foo", update);
var completed = false;
target.Subscribe(_ => { }, () => completed = true);
update.OnCompleted();
Assert.True(completed);
}
示例15: Run_not_preferred
public void Run_not_preferred()
{
ConsoleUtility.PrintSuccess(string.Format("Observable create example - not preferred"));
var topic = new Subject<Price>();
var subscription = topic.Subscribe(
price => Console.WriteLine(price.ToString()));
topic.OnNext(new Price(10, 1, 2, 3));
topic.OnCompleted();
subscription.Dispose();
}