From c1f46799c20406946c1abc7404fc5badeeee2c27 Mon Sep 17 00:00:00 2001 From: Brandon Date: Mon, 9 Nov 2026 16:12:46 -0800 Subject: [PATCH 1/3] change error message for unsupported microscope geometries --- .../org/micromanager/lightsheetmanager/LightSheetManager.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/micromanager/lightsheetmanager/LightSheetManager.java b/src/main/java/org/micromanager/lightsheetmanager/LightSheetManager.java index 0794d4e0..f921843d 100644 --- a/src/main/java/org/micromanager/lightsheetmanager/LightSheetManager.java +++ b/src/main/java/org/micromanager/lightsheetmanager/LightSheetManager.java @@ -84,7 +84,7 @@ public boolean setup() { acqEngine_ = new AcquisitionEngineDispim(this); break; default: - final String message = "AcquisitionEngine not implemented for " + geometryType; + final String message = "Microscope geometry type " + geometryType + " is not supported yet."; studio_.logs().logError(message); errorText_ = message; return false; // early exit => show error ui From 56cf6cc9ac732821c79f8255ba1afed500250144 Mon Sep 17 00:00:00 2001 From: Brandon Date: Wed, 11 Nov 2026 15:35:33 -0800 Subject: [PATCH 2/3] save the settings when micro-manager exits with the plugin window open --- .../LightSheetManagerPlugin.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/main/java/org/micromanager/lightsheetmanager/LightSheetManagerPlugin.java b/src/main/java/org/micromanager/lightsheetmanager/LightSheetManagerPlugin.java index 0584a6fa..bb08d74b 100644 --- a/src/main/java/org/micromanager/lightsheetmanager/LightSheetManagerPlugin.java +++ b/src/main/java/org/micromanager/lightsheetmanager/LightSheetManagerPlugin.java @@ -1,5 +1,7 @@ package org.micromanager.lightsheetmanager; +import com.google.common.eventbus.Subscribe; +import org.micromanager.events.ShutdownCommencingEvent; import org.micromanager.lightsheetmanager.gui.utils.WindowUtils; import org.micromanager.MenuPlugin; import org.micromanager.Studio; @@ -76,6 +78,9 @@ public void onPluginSelected() { // clear references after the window is fully closed // prevent memory leaks from closed plugin instances WindowUtils.registerWindowClosedEvent(frame_, event -> { + if (isLoaded) { + studio_.events().unregisterForEvents(this); + } frame_ = null; model_ = null; }); @@ -83,6 +88,11 @@ public void onPluginSelected() { frame_.pack(); frame_.setVisible(true); frame_.toFront(); + + // quitting Micro-Manager does not close this window, so save the settings on exit too + if (isLoaded) { + studio_.events().registerForEvents(this); + } } catch (Exception e) { // cleanup resources frame_ = null; @@ -93,6 +103,22 @@ public void onPluginSelected() { } } + /** + * Saves the settings when Micro-Manager shuts down, before it writes the user profile to disk. + */ + @Subscribe + public void onShutdownCommencing(final ShutdownCommencingEvent event) { + if (event.isCanceled()) { + return; // early exit => the shutdown was canceled + } + try { + model_.userSettings().save(); + studio_.logs().logMessage("User settings saved because Micro-Manager is shutting down."); + } catch (RuntimeException e) { + studio_.logs().logError(e, "Failed to save user settings while Micro-Manager was shutting down."); + } + } + @Override public String getName() { return menuName; From ae659f7529f0c791a68b44778a5cb4660bbec4ee Mon Sep 17 00:00:00 2001 From: Brandon Date: Wed, 11 Nov 2026 17:35:34 -0800 Subject: [PATCH 3/3] stop the position polling when micro-manager exits --- .../model/positions/PositionUpdater.java | 64 ++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/micromanager/lightsheetmanager/model/positions/PositionUpdater.java b/src/main/java/org/micromanager/lightsheetmanager/model/positions/PositionUpdater.java index a865d1f6..f4937e88 100644 --- a/src/main/java/org/micromanager/lightsheetmanager/model/positions/PositionUpdater.java +++ b/src/main/java/org/micromanager/lightsheetmanager/model/positions/PositionUpdater.java @@ -1,14 +1,19 @@ package org.micromanager.lightsheetmanager.model.positions; +import com.google.common.eventbus.Subscribe; import mmcorej.DeviceType; +import org.micromanager.events.ShutdownCommencingEvent; import org.micromanager.lightsheetmanager.LightSheetManager; import java.util.ArrayList; import java.util.HashMap; import java.util.Objects; +import java.util.concurrent.ExecutionException; import java.util.concurrent.Executors; +import java.util.concurrent.RejectedExecutionException; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; import java.util.concurrent.atomic.AtomicBoolean; public class PositionUpdater implements Publisher { @@ -16,6 +21,10 @@ public class PositionUpdater implements Publisher { // each poll reads every stage and galvo over serial, so do not poll faster than this private static final int MIN_POLLING_DELAY_MS = 500; + // the most the exit waits for a poll that is reading the devices, so a stuck poll + // cannot hang Micro-Manager's shutdown + private static final long EXIT_WAIT_MS = 2000; + // polling private volatile int pollingDelayMs_; private volatile boolean isPolling_; @@ -24,6 +33,10 @@ public class PositionUpdater implements Publisher { private final ScheduledExecutorService executor_; private final AtomicBoolean isTicking_; // true once the task is scheduled + // stops the polling when Micro-Manager shuts down + private final ExitListener exitListener_ = new ExitListener(); + private final AtomicBoolean isListeningForExit_ = new AtomicBoolean(false); + // data private final HashMap positions_; private final HashMap> topics_; @@ -73,6 +86,9 @@ public void startPolling() { // the thread starts on first use: close() is skipped when the plugin fails to load, // so a thread started any earlier could never be shut down if (isTicking_.compareAndSet(false, true)) { + // the thread calls the core, which Micro-Manager deletes when it shuts down + model_.studio().events().registerForEvents(exitListener_); + isListeningForExit_.set(true); executor_.schedule(this::tick, 0, TimeUnit.MILLISECONDS); } } @@ -86,11 +102,15 @@ public boolean isPolling() { } /** - * Stops polling and ends the polling thread. Call when the plugin closes. + * Stops polling, ends the polling thread and stops listening for Micro-Manager's exit. + * Call when the plugin closes. */ public void shutdown() { isPolling_ = false; executor_.shutdown(); + if (isListeningForExit_.compareAndSet(true, false)) { + model_.studio().events().unregisterForEvents(exitListener_); + } } public void setPollingDelayMs(final int delayMs) { @@ -143,4 +163,46 @@ private void updateSubscribers() { } } + /** + * Waits until no poll is reading the devices, for at most {@code EXIT_WAIT_MS}. + */ + private void waitForPollInProgress() { + if (!isTicking_.get()) { + return; // the thread never started + } + try { + // the polling thread runs this once the poll in progress, if any, has ended + executor_.submit(() -> { }).get(EXIT_WAIT_MS, TimeUnit.MILLISECONDS); + } catch (TimeoutException e) { + model_.studio().logs().logError("Position polling was still reading the devices " + + EXIT_WAIT_MS + " ms after Micro-Manager started to shut down."); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } catch (ExecutionException | RejectedExecutionException e) { + // shutdown() already ran, so the polling is off + } + } + + /** + * Stops the polling before Micro-Manager deletes the core: polling a deleted core crashes + * Micro-Manager. If a later subscriber cancels the exit, the polling stays off. + */ + private final class ExitListener { + + @Subscribe + public void onShutdownCommencing(final ShutdownCommencingEvent event) { + // another subscriber canceled the exit before this one ran, so the core stays + if (event.isCanceled()) { + return; + } + final boolean wasPolling = isPolling_; + isPolling_ = false; + waitForPollInProgress(); + if (wasPolling) { + model_.studio().logs().logMessage( + "Stopped position polling because Micro-Manager is shutting down."); + } + } + } + }