@@ -65,6 +65,7 @@ export default class Activator {
6565 private commandExecuting = false ;
6666
6767 private disableExtWarning = false ;
68+ private statusbarMsgDisposable ?: vscode . Disposable ;
6869
6970 constructor ( ) {
7071 this . logger = new Logger ( "Activator" ) ;
@@ -542,13 +543,16 @@ export default class Activator {
542543 }
543544 }
544545
546+ if ( await this . checkForRunningOperation ( ) ) {
547+ return ;
548+ }
549+
545550 const forceDisableSoftReset =
546551 this . settings ?. getBoolean ( SettingsKey . noSoftResetOnRun ) ?? false ;
547552
548553 if ( ! noSoftReset && ! forceDisableSoftReset ) {
549554 await PicoMpyCom . getInstance ( ) . softReset ( ) ;
550555 }
551- await focusTerminal ( this . terminalOptions ) ;
552556 // TODO: maybe freeze terminal until this operation runs to prevent user input
553557 const data = await PicoMpyCom . getInstance ( ) . runFile (
554558 file ,
@@ -557,6 +561,7 @@ export default class Activator {
557561 return ;
558562 }
559563
564+ void focusTerminal ( this . terminalOptions ) ;
560565 this . commandExecuting = true ;
561566 this . terminal ?. cleanAndStore ( ) ;
562567 this . ui ?. userOperationStarted ( ) ;
@@ -610,6 +615,11 @@ export default class Activator {
610615
611616 return ;
612617 }
618+
619+ if ( await this . checkForRunningOperation ( ) ) {
620+ return ;
621+ }
622+
613623 const forceDisableSoftReset =
614624 this . settings ?. getBoolean ( SettingsKey . noSoftResetOnRun ) ?? false ;
615625
@@ -2172,4 +2182,47 @@ export default class Activator {
21722182 ) ;
21732183 }
21742184 }
2185+
2186+ /**
2187+ * Checks if there is a running operation and asks the user if it should be canceled.
2188+ *
2189+ * @returns `true` if the user does not want to cancel the already running
2190+ * operation, `false` otherwise.
2191+ */
2192+ private async checkForRunningOperation ( ) : Promise < boolean > {
2193+ if ( this . commandExecuting ) {
2194+ // ask user if it want to cancel running operation or cancel the new one
2195+ const choice = await vscode . window . showWarningMessage (
2196+ "An operation is already running. Do you want to cancel it?" ,
2197+ {
2198+ modal : true ,
2199+ } ,
2200+ "Yes" ,
2201+ "No"
2202+ ) ;
2203+
2204+ if ( choice === "Yes" ) {
2205+ if ( this . commandExecuting ) {
2206+ PicoMpyCom . getInstance ( ) . interruptExecution ( ) ;
2207+
2208+ // wait for 500ms for operation to stop and clean up local state
2209+ await new Promise ( resolve => setTimeout ( resolve , 500 ) ) ;
2210+ }
2211+ } else {
2212+ if ( this . statusbarMsgDisposable ) {
2213+ this . statusbarMsgDisposable . dispose ( ) ;
2214+ }
2215+
2216+ // void vscode.window.showWarningMessage("Operation canceled.");
2217+ this . statusbarMsgDisposable = vscode . window . setStatusBarMessage (
2218+ "Operation canceled." ,
2219+ 5000
2220+ ) ;
2221+
2222+ return true ;
2223+ }
2224+ }
2225+
2226+ return false ;
2227+ }
21752228}
0 commit comments