From 3225fdf9d898ecd9b7704a5add237c144d36d302 Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Wed, 22 Mar 2017 11:46:41 +0100 Subject: [PATCH] Avoid board change during compilation/upload By threading the boardChange callback we can busy wait until the compilation/upload phase has ended and change the board when done. Fixes #6035 --- app/src/processing/app/Base.java | 26 ++++++++++++++++++++------ app/src/processing/app/Editor.java | 4 ++++ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/app/src/processing/app/Base.java b/app/src/processing/app/Base.java index ab7d379ef44..1d38c590a30 100644 --- a/app/src/processing/app/Base.java +++ b/app/src/processing/app/Base.java @@ -1310,7 +1310,7 @@ public void rebuildExamplesMenu(JMenu menu) { private static String priorPlatformFolder; private static boolean newLibraryImported; - public void onBoardOrPortChange() { + public synchronized void onBoardOrPortChange() { BaseNoGui.onBoardOrPortChange(); // reload keywords when package/platform changes @@ -1509,12 +1509,26 @@ private JRadioButtonMenuItem createBoardMenusAndCustomMenus( @SuppressWarnings("serial") Action action = new AbstractAction(board.getName()) { public void actionPerformed(ActionEvent actionevent) { - BaseNoGui.selectBoard((TargetBoard) getValue("b")); - filterVisibilityOfSubsequentBoardMenus(boardsCustomMenus, (TargetBoard) getValue("b"), 1); - onBoardOrPortChange(); - rebuildImportMenu(Editor.importMenu); - rebuildExamplesMenu(Editor.examplesMenu); + new Thread() + { + public void run() { + if (activeEditor != null && activeEditor.isCompiling()) { + // block until isCompiling becomes false, but aboid blocking the UI + while (activeEditor.isCompiling()) { + try { + Thread.sleep(100); + } catch (InterruptedException e) {} + } + } + + BaseNoGui.selectBoard((TargetBoard) getValue("b")); + filterVisibilityOfSubsequentBoardMenus(boardsCustomMenus, (TargetBoard) getValue("b"), 1); + onBoardOrPortChange(); + rebuildImportMenu(Editor.importMenu); + rebuildExamplesMenu(Editor.examplesMenu); + } + }.start(); } }; action.putValue("b", board); diff --git a/app/src/processing/app/Editor.java b/app/src/processing/app/Editor.java index dd7759471bc..f473f50dfb6 100644 --- a/app/src/processing/app/Editor.java +++ b/app/src/processing/app/Editor.java @@ -2266,6 +2266,10 @@ public void run() { } } + public boolean isCompiling() { + return uploading; + } + private void resumeOrCloseSerialMonitor() { // Return the serial monitor window to its initial state if (serialMonitor != null) {