public void SingletonComponentRegisteredWithComponentInstance_IsNotDisposed() { // this component instance is externally-owned var disposed = false; var immortalServer = new DisposableComponent { Handler = () => disposed = true }; var cat = new ComponentCatalog(); cat.RegisterComponent <ISampleComponent, DisposableComponent>(immortalServer); Assert.IsFalse(disposed); var instance = cat.GetComponent <ISampleComponent>(); AssertEx.IsInstanceOf <DisposableComponent>(instance); var reg = cat.GetRegistration(typeof(ISampleComponent)); cat.CleanUpComponentInstance(reg, instance); Assert.IsFalse(disposed); immortalServer.Dispose(); Assert.IsTrue(disposed); }
public TimerFontContainer([CanBeNull] ref IContainer components) { fontCollectionWrapper = new DisposableComponent<PrivateFontCollection>(new PrivateFontCollection(), ref components); InitializeTimerFont(); }
public EmulatorForm([NotNull] StartupArguments startupArguments) { this.startupArguments = startupArguments; InitializeComponent(); Text += AssemblyReader.GetInformationalVersion(); sessionManager = new DisposableComponent<CirceMediatorSessionManager>(new CirceMediatorSessionManager(), ref components); }
public static void Register(FormWithWindowStateChangeEvent form, IWindowSettings settings, bool initiallyMaximized, ref IContainer?container) { Guard.NotNull(form, nameof(form)); Guard.NotNull(settings, nameof(settings)); if (!form.IsMdiChild) { throw new InvalidOperationException("Form must be an MDI child."); } // Justification for discard: Object registers itself to get disposed along with specified container on Form. _ = new DisposableComponent <TargetWindow>(new TargetWindow(form, settings, initiallyMaximized), ref container); }
public void Disposable_components_are_decommissioned_on_container_Dispose_all_threads() { Container.Register(Component.For <DisposableComponent>().LifeStyle.PerThread); var a1 = Container.Resolve <DisposableComponent>(); DisposableComponent a2 = null; ExecuteOnAnotherThreadAndWait(() => a2 = Container.Resolve <DisposableComponent>()); Container.Dispose(); Assert.IsTrue(a1.Disposed); Assert.IsTrue(a2.Disposed); }
public void Dispose_ShouldDisposeDisposableComponent() { // Arrange var component = new DisposableComponent(); var supervisor = new ServerSupervisor(component, _options); supervisor.Start(); Thread.Sleep(100); // Act supervisor.Dispose(); Thread.Sleep(100); // Assert Assert.True(component.Disposed); }
// Update UI state. void SettingChanging(object sender, SettingChangedEventArgs e) { if (e.SettingName == "IgnoreAccents" || e.SettingName == "IgnoreCase") { // This shouldn't fire the CheckedChanged/SettingChanged events in an infinite loop. if (e.SettingName == "IgnoreAccents") { ignoreAccentsMenuItem.Checked = ignoreAccentsCheck.Checked = GuiConfiguration.IgnoreAccents; } else if (e.SettingName == "IgnoreCase") { ignoreCaseMenuItem.Checked = ignoreCaseCheck.Checked = GuiConfiguration.IgnoreCase; } // We might want to skip this if nothing was actually changed. UpdateResults(); } else if (e.SettingName == "ListFontName" || e.SettingName == "ListFontSize") { // Note: this is slightly inefficient -- if both are set at once it redisplays twice. Font disposeOf = null; if (listFontComponent != null) { components.Remove(listFontComponent); disposeOf = grid.Font; } var font = GuiConfiguration.GetListFont(); if (font != null) { if (defaultGridFont == null) { defaultGridFont = grid.Font; } listFontComponent = new DisposableComponent(font); grid.Font = font; } else { grid.Font = defaultGridFont; } if (disposeOf != null) { disposeOf.Dispose(); } AdjustGridRowHeight(); } }
public QuizletSetSelector() { InitializeComponent(); importer = new QuizletImporter(); if (components == null) { components = new System.ComponentModel.Container(); } cts = new CancellationTokenSource(); components.Add(disposableComponent = new DisposableComponent(cts)); searching = false; importButton.Enabled = false; searchResults.SelectedIndexChanged += SearchResultsSelectedIndexChanged; }
public LookupForm(IBilingualDictionary dictionary) : this() { components.Add(new DisposableComponent(new LookupFormFileIsInUse(this, dictionary.Path))); components.Add(new DisposableComponent(dictionary)); Dictionary = dictionary; var mru = GuiConfiguration.RecentDictionaries ?? new MruList <DictionaryInfo>(10); mru.Update(dictionary.Info); GuiConfiguration.RecentDictionaries = mru; Font listFont = GuiConfiguration.GetListFont(); if (listFont != null) { components.Add(listFontComponent = new DisposableComponent(listFont)); defaultGridFont = grid.Font; grid.Font = listFont; } // TODO: This really needs testing. It could make things completely unusable... // I can't even remember if they're used... try { if (dictionary.FirstLanguageCode != null && dictionary.SecondLanguage != null) { sourceCulture = new CultureInfo(dictionary.FirstLanguageCode); targetCulture = new CultureInfo(dictionary.SecondLanguageCode); } } catch (ArgumentException) { // One of the cultures wasn't supported. In that case, set both cultures to null, // because it isn't worth having only one. sourceCulture = targetCulture = null; } InitialiseView(); Load += delegate { UpdateResults(); }; Show(); }
public LookupForm(IBilingualDictionary dictionary) : this() { components.Add(new DisposableComponent(new LookupFormFileIsInUse(this, dictionary.Path))); components.Add(new DisposableComponent(dictionary)); Dictionary = dictionary; var mru = GuiConfiguration.RecentDictionaries ?? new MruList<DictionaryInfo>(10); mru.Update(dictionary.Info); GuiConfiguration.RecentDictionaries = mru; Font listFont = GuiConfiguration.GetListFont(); if (listFont != null) { components.Add(listFontComponent = new DisposableComponent(listFont)); defaultGridFont = grid.Font; grid.Font = listFont; } // TODO: This really needs testing. It could make things completely unusable... // I can't even remember if they're used... try { if (dictionary.FirstLanguageCode != null && dictionary.SecondLanguage != null) { sourceCulture = new CultureInfo(dictionary.FirstLanguageCode); targetCulture = new CultureInfo(dictionary.SecondLanguageCode); } } catch (ArgumentException) { // One of the cultures wasn't supported. In that case, set both cultures to null, // because it isn't worth having only one. sourceCulture = targetCulture = null; } InitialiseView(); Load += delegate { UpdateResults(); }; Show(); }
public void Dispose_ShouldDisposeDisposableComponent() { // Arrange _options.MaxRetryAttempts = 0; var component = new DisposableComponent(); var supervisor = new ServerSupervisor(component, _options); supervisor.Start(); Thread.Sleep(100); // Act supervisor.Dispose(); Thread.Sleep(500); // Assert Assert.True(component.Disposed); }
public void SingletonComponentRegisteredWithComponentInstance_IsNotDisposed() { // this component instance is externally-owned var disposed = false; using (var immortalServer = new DisposableComponent { Handler = () => disposed = true }) using (var cat = new ComponentCatalog()) { cat.RegisterComponent<ISampleComponent, DisposableComponent>(immortalServer); Assert.IsFalse(disposed); var instance = cat.GetComponent<ISampleComponent>(); AssertEx.IsInstanceOf<DisposableComponent>(instance); var reg = cat.GetRegistration(typeof(ISampleComponent)); cat.CleanUpComponentInstance(reg, instance); Assert.IsFalse(disposed); immortalServer.Dispose(); Assert.IsTrue(disposed); } }
public MainForm() { InitializeComponent(); EnsureHandleCreated(); Text += AssemblyReader.GetInformationalVersion(); // ReSharper disable once ObjectCreationAsStatement // Reason: Assignment is unneeded because this registers a callback to the allocated object. new DisposableComponent<DisposableHolder>(new DisposableHolder(EmulatorProcessKill), ref components); var runVisualizer = new CompositeRunVisualizer(new ICompetitionRunVisualizer[] { new WinFormsVisualizer(this, competitionStateOverview), new WinFormsVisualizer(this, timerDisplayForm), wirelessRunVisualizer }); classController = new DisposableComponent<CompetitionClassController>( new CompetitionClassController(() => healthMonitor.GetLatest(), clockSynchronizationMonitor, runVisualizer), ref components); #if DEBUG logLinkLabel.Visible = true; #endif emulatorLinkLabel.Visible = HasEmulatorFiles(); logForm.VisibleChanged += (s, e) => { logLinkLabel.Text = logForm.Visible ? "Hide log" : "Show log"; }; networkSetupForm = new NetworkSetupForm(this); healthMonitor.HealthChanged += (s, e) => this.EnsureOnMainThread(() => { UpdateControlsEnabledStateAfterHealthChanged(e.Argument); ShowNetworkHealth(e.Argument); }); classController.Component.UnknownErrorDuringSave += (s, e) => this.EnsureOnMainThread(() => ShowError(e.Argument.ToString())); classController.Component.Aborted += (s, e) => this.EnsureOnMainThread(ClassControllerOnAborted); classController.Component.RankingChanged += (s, e) => this.EnsureOnMainThread(() => ClassControllerOnRankingChanged(e)); classController.Component.StateTransitioned += (s, e) => this.EnsureOnMainThread(() => stateVisualizer.TransitionTo(e.Argument)); networkStatusView.SwitchToStatusMode(); actionAdapter.CommandReceived += (s, e) => classController.Component.HandleCommand(e.Command); actionAdapter.GatePassed += (s, e) => classController.Component.HandleGatePassed(e.SensorTime, e.GatePassage, e.Source); numberFilter.NotifyCompetitorSelecting += (s, e) => classController.Component.StartCompetitorSelection(e.IsCurrentCompetitor); numberFilter.NotifyDigitReceived += (s, e) => classController.Component.ReceiveDigit(e.IsCurrentCompetitor, e.CompetitorNumber); numberFilter.NotifyCompetitorSelectCanceled += (s, e) => classController.Component.CompleteCompetitorSelection(e.IsCurrentCompetitor, null); numberFilter.NotifyCompetitorSelected += (s, e) => classController.Component.CompleteCompetitorSelection(e.IsCurrentCompetitor, e.CompetitorNumber); numberFilter.NotifyUnknownAction += (s, e) => actionAdapter.Adapt(e.Source, e.Key, e.SensorTime); keyTracker.ModifierKeyDown += (s, e) => numberFilter.HandleModifierKeyDown(e.Source, e.Modifier); keyTracker.KeyDown += (s, e) => numberFilter.HandleKeyDown(e.Source, e.Key, e.SensorTime); keyTracker.ModifierKeyUp += (s, e) => numberFilter.HandleModifierKeyUp(e.Source, e.Modifier); keyTracker.MissingKey += (s, e) => numberFilter.HandleMissingKey(e.Source, e.SensorTime); sessionManager = new DisposableComponent<CirceControllerSessionManager>( new CirceControllerSessionManager(), ref components); sessionManager.Component.PacketSending += (s, e) => this.EnsureOnMainThread(() => logForm.PulseOutputLed()); sessionManager.Component.PacketReceived += (s, e) => this.EnsureOnMainThread(() => logForm.PulseInputLed()); sessionManager.Component.ConnectionStateChanged += (s, e) => { healthMonitor.HandleConnectionStateChanged(e.State); this.EnsureOnMainThread( () => { networkStatusView.IsConnected = e.State == ControllerConnectionState.Connected; }); }; sessionManager.Component.DeviceActionReceived += (s, e) => keyTracker.ProcessDeviceAction(e.Argument); sessionManager.Component.DeviceTracker.DeviceAdded += (s, e) => healthMonitor.HandleDeviceAdded(e.Argument); sessionManager.Component.DeviceTracker.DeviceChanged += (s, e) => healthMonitor.HandleDeviceChanged(e.Argument); sessionManager.Component.DeviceTracker.DeviceRemoved += (s, e) => healthMonitor.HandleDeviceRemoved(e.Argument); sessionManager.Component.DeviceTracker.MediatorStatusChanged += (s, e) => healthMonitor.HandleMediatorStatusChanged(e.Argument); clockSynchronizationMonitor.Initialize(sessionManager.Component); sessionManager.Component.DeviceTracker.DeviceAdded += (s, e) => this.EnsureOnMainThread(() => networkStatusView.AddOrUpdate(e.Argument)); sessionManager.Component.DeviceTracker.DeviceChanged += (s, e) => this.EnsureOnMainThread(() => networkStatusView.AddOrUpdate(e.Argument)); sessionManager.Component.DeviceTracker.DeviceRemoved += (s, e) => this.EnsureOnMainThread(() => networkStatusView.Remove(e.Argument)); networkSetupForm.SessionManager = sessionManager.Component; }
// Update UI state. void SettingChanging(object sender, SettingChangedEventArgs e) { if (e.SettingName == "IgnoreAccents" || e.SettingName == "IgnoreCase") { // This shouldn't fire the CheckedChanged/SettingChanged events in an infinite loop. if (e.SettingName == "IgnoreAccents") ignoreAccentsMenuItem.Checked = ignoreAccentsCheck.Checked = GuiConfiguration.IgnoreAccents; else if (e.SettingName == "IgnoreCase") ignoreCaseMenuItem.Checked = ignoreCaseCheck.Checked = GuiConfiguration.IgnoreCase; // We might want to skip this if nothing was actually changed. UpdateResults(); } else if (e.SettingName == "ListFontName" || e.SettingName == "ListFontSize") { // Note: this is slightly inefficient -- if both are set at once it redisplays twice. Font disposeOf = null; if (listFontComponent != null) { components.Remove(listFontComponent); disposeOf = grid.Font; } var font = GuiConfiguration.GetListFont(); if (font != null) { if (defaultGridFont == null) defaultGridFont = grid.Font; listFontComponent = new DisposableComponent(font); grid.Font = font; } else { grid.Font = defaultGridFont; } if (disposeOf != null) disposeOf.Dispose(); AdjustGridRowHeight(); } }
public void DisposableComponentTest() => Assert.IsTrue(DisposableComponent.Assert(ITERATION_CNT));