public ModalDialogContext(string title = null) { var activeView = new SerialDisposable(); ModalDialogView dialog = null;// new ModalDialogView(); var dispatcher = Application.Current.Dispatcher; //var dispatcher = dialog.Dispatcher; dispatcher.BeginInvoke(() => { dialog = new ModalDialogView(); if (!disposables.IsDisposed) { disposables.Add( Disposable.Create( () => dispatcher.BeginInvoke( ()=>dialog.Close() ) ) ); dialog.Header = title; dialog.Owner = Application.Current.MainWindow; dialog.WindowStartupLocation = WindowStartupLocation.CenterOwner; dialog.SizeToContent = SizeToContent.WidthAndHeight; dialog.ShowInTaskbar = false; dialog.ShowDialog(); activeView.Dispose(); } }); var presenter = ViewPresenter.Create(view => { var disp = new CompositeDisposable(); activeView.Disposable = disp; dispatcher.BeginInvoke(()=>{ dbg.Assert(dialog != null); if (!disp.IsDisposed) { dbg.Assert(dialog.Content == null); dialog.Content = view; var header = NavigationContext.GetTitle(view); dialog.Header = header ?? title; disp.Add( Disposable.Create(() => { dispatcher.BeginInvoke(() => { dbg.Assert(dialog.Content == view); dialog.Header = title; dialog.Content = null; var d = view as IDisposable; if (d != null) { d.Dispose(); } }); })); } }); return disp; }); container.RegisterInstance<IViewPresenter>(presenter); }
public void SerialRxOfficial() { var d = new SerialDisposable(); d.IsDisposed.IsFalse(); var id1 = new IdDisp(1); var id2 = new IdDisp(2); var id3 = new IdDisp(3); // dispose first d.Dispose(); d.IsDisposed.IsTrue(); d.Disposable = id1; id1.IsDisposed.IsTrue(); d.Disposable = id2; id2.IsDisposed.IsTrue(); d.Disposable = id3; id3.IsDisposed.IsTrue(); // normal flow d = new SerialDisposable(); id1 = new IdDisp(1); id2 = new IdDisp(2); id3 = new IdDisp(3); d.Disposable = id1; id1.IsDisposed.IsFalse(); d.Dispose(); id1.IsDisposed.IsTrue(); d.Disposable = id2; id2.IsDisposed.IsTrue(); d.Disposable = id3; id3.IsDisposed.IsTrue(); // exception flow d = new SerialDisposable(); id1 = new IdDisp(1); id2 = new IdDisp(2); id3 = new IdDisp(3); d.Disposable = id1; id1.IsDisposed.IsFalse(); d.Disposable = id2; id1.IsDisposed.IsTrue(); id2.IsDisposed.IsFalse(); d.Disposable = id3; id2.IsDisposed.IsTrue(); id3.IsDisposed.IsFalse(); d.Dispose(); id3.IsDisposed.IsTrue(); // null d = new SerialDisposable(); id1 = new IdDisp(1); d.Disposable = null; d.Dispose(); d.Disposable = null; }
public void SerialDisposable_Dispose() { var disp = false; var m = new SerialDisposable(); var d = Disposable.Create(() => { disp = true; }); m.Disposable = d; Assert.AreSame(d, m.Disposable); Assert.IsFalse(disp); m.Dispose(); Assert.IsTrue(m.IsDisposed); Assert.IsTrue(disp); //Assert.IsNull(m.Disposable); // BREAKING CHANGE v2 > v1.x - Undefined behavior after disposal. }
public void SerialDisposable_ReplaceAfterDispose() { var disp1 = false; var disp2 = false; var m = new SerialDisposable(); m.Dispose(); Assert.IsTrue(m.IsDisposed); var d1 = Disposable.Create(() => { disp1 = true; }); m.Disposable = d1; Assert.IsNull(m.Disposable); // CHECK Assert.IsTrue(disp1); var d2 = Disposable.Create(() => { disp2 = true; }); m.Disposable = d2; Assert.IsNull(m.Disposable); // CHECK Assert.IsTrue(disp2); }
public void Return_DisposedAfterNext() { var scheduler = new TestScheduler(); var d = new SerialDisposable(); var xs = Observable.Return(42, scheduler); var res = scheduler.CreateObserver<int>(); scheduler.ScheduleAbsolute(100, () => d.Disposable = xs.Subscribe( x => { d.Dispose(); res.OnNext(x); }, res.OnError, res.OnCompleted ) ); scheduler.Start(); res.Messages.AssertEqual( OnNext(101, 42) ); }
static void Impl(int disposeAt) { var rand = new Random(); var s = new SerialDisposable(); Console.Write("Dispose @ = {0} - ", disposeAt); if (disposeAt == 0) { s.Dispose(); Console.Write("{SD} "); } if (disposeAt == 1) { var sleep = rand.Next(0, 5) > 1 /* 60% chance */ ? rand.Next(2, 1000) : 0; ThreadPool.QueueUserWorkItem(_ => { Helpers.SleepOrSpin(sleep); s.Dispose(); Console.Write("{SD} "); }); } var n = rand.Next(0, 1000); var cd = new CountdownEvent(n); var ds = Enumerable.Range(0, n).Select(_ => Disposable.Create(() => cd.Signal())).ToArray(); var m = rand.Next(1, 100); var jobs = ds.GroupBy(_ => rand.Next() % m).Select(Enumerable.ToList).ToList(); Console.Write("N = {0}, M = {1} - ", n, m); var done = new CountdownEvent(jobs.Count); foreach (var job in jobs) { var sleep = rand.Next(0, 10) == 0 /* 10% chance */ ? rand.Next(2, 100) : 0; var sleepAt = Enumerable.Range(0, rand.Next(0, job.Count) / rand.Next(1, 100)).ToArray(); var sleeps = sleepAt.Select(_ => rand.Next(0, 50)).ToArray(); var rem = rand.Next(0, 3) == 0; /* 33% chance */ var remAt = rand.Next(0, 10) == 0 /* 10% chance */ ? rand.Next(2, 100) : 0; var mine = job; ThreadPool.QueueUserWorkItem(_ => { Helpers.SleepOrSpin(sleep); var j = 0; foreach (var d in mine) { if (sleepAt.Contains(j)) Helpers.SleepOrSpin(sleeps[j]); s.Disposable = d; Console.Write("+"); j++; } done.Signal(); }); } done.Wait(); if (disposeAt == 2) { s.Dispose(); Console.Write("{SD} "); } cd.Wait(); Console.WriteLine("."); }