swingworker.cancel() is giving errors. Attempting to use “stop button” to stop the code from running - MM1.4

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

swingworker.cancel() is giving errors. Attempting to use “stop button” to stop the code from running - MM1.4

SaralaT

Hi everyone,


I have a problem with using the swingworker to add a functional stop button to a set of code. Once pressed I'd like the code to stop and exit. How ever, I am new to the swingworker itself, and the stop button I placed seems to give me a Java error in a micromanager error window. My code snippet is as below (as I am relatively new to Java programming, please excuse my untidy and perhaps not so efficient code set). Here once the program starts running, it successfully prints out "TRACKSPTINSTANT EXECUTE - OK", and goes on to do what is indicated inside the  track(). While it is running, when I press "stop button", how ever, it gives the error as below, and then once I press ok , it just goes on to print "TRACKSPTINSTANT CANCEL - OK" and continues with the rest of the code, without stopping. I am unsure of why this happens. Would be great if some one could help me with some advise on how I may go ahead to correct this problem. What does this error mean? Is there a more simple way of adding a stop button to do this same function that I am trying to do? 


Thanks a lot in advance!






////lot of code before not shown.....
ActionListener trackButtonPressed = new ActionListener() {           
                @Override
                public void actionPerformed(ActionEvent event){
                        boolean ask3D = true;

                        trackSPT(ask3D);
                        }    
                 };       

public void trackSPT(boolean ask3D) {            
                trackSPTInstant = new trackSPTWorker(ask3D);
                trackSPTInstant.execute();
                IJ.log("TRACKSPTINSTANT EXECUTE - OK");
                }  

ActionListener stopButtonPressed = new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent event){
                       trackSPTInstant.cancel(true);//this is somehow not working... why.....
                IJ.log("TRACKSPTINSTANT CANCEL - OK");
                }
                };

public class trackSPTWorker extends SwingWorker<Void, Void> {
                        private final boolean ask3D;
                        public trackSPTWorker(final boolean ask3D) {
                        this.ask3D = ask3D;
                          }


                        @Override
                          protected Void doInBackground() throws Exception {
                          if (ask3D) {
                                 stopButton.setEnabled(true);

                                 track();//the method which holds everything that the trackButton should do!!!
                                }

                          else {
                                stopButton.setEnabled(true);

                                }

                          return null;
                        }
                   }
///lot of code after not shown......


Best Regards,

Sarala



_______________________________________________
micro-manager-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/micro-manager-general
Reply | Threaded
Open this post in threaded view
|

Re: swingworker.cancel() is giving errors. Attempting to use “stop button” to stop the code from running - MM1.4

Stuurman, Nico
Hi Sarala,

This is more a general Java question, probably best asked on
StackOverflow or similar.  However, I would have a look at:
https://docs.oracle.com/javase/tutorial/uiswing/concurrency/cancel.html.
It looks to me that your call "trackSPTInstant.cancel(true);", results
in an interrupt in your Swingworker that is not handles (caught) by your
SwingWorker (although it is confusing that you do not get an
"Interrupted" exception).  You could either try to catch the Interrupt
in your SwingWorker, or call " trackSPTInstant.cancel(false);", and
check SwingWorker.isCancelled regularly in your code.

Hope this helps!

Best,

Nico



> Hi everyone,
>
>
> I have a problem with using the swingworker to add a functional stop
> button to a set of code. Once pressed I'd like the code to stop and
> exit. How ever, I am new to the swingworker itself, and the stop
> button I placed seems to give me a Java error in a micromanager error
> window. My code snippet is as below (as I am relatively new to Java
> programming, please excuse my untidy and perhaps not so efficient code
> set). Here once the program starts running, it successfully prints out
> "TRACKSPTINSTANT EXECUTE - OK", and goes on to do what is indicated
> inside the  track(). While it is running, when I press "stop button",
> how ever, it gives the error as below, and then once I press ok , it
> just goes on to print "TRACKSPTINSTANT CANCEL - OK" and continues with
> the rest of the code, without stopping. I am unsure of why this
> happens. Would be great if some one could help me with some advise on
> how I may go ahead to correct this problem. What does this error mean?
> Is there a more simple way of adding a stop button to do this same
> function that I am trying to do?
>
>
> Thanks a lot in advance!
>
>
>
>
>
>
> ////lot of code before not shown.....
> ActionListener trackButtonPressed = new ActionListener() {
>                 @Override
>                 public void actionPerformed(ActionEvent event){
>                         boolean ask3D = true;
>
>                         trackSPT(ask3D);
>                         }
>                  };
>
> public void trackSPT(boolean ask3D) {
>                 trackSPTInstant = new trackSPTWorker(ask3D);
>                 trackSPTInstant.execute();
>                 IJ.log("TRACKSPTINSTANT EXECUTE - OK");
>                 }
>
> ActionListener stopButtonPressed = new ActionListener() {
>                 @Override
>                 public void actionPerformed(ActionEvent event){
>                        trackSPTInstant.cancel(true);//this is somehow
> not working... why.....
>                 IJ.log("TRACKSPTINSTANT CANCEL - OK");
>                 }
>                 };
>
> public class trackSPTWorker extends SwingWorker<Void, Void> {
>                         private final boolean ask3D;
>                         public trackSPTWorker(final boolean ask3D) {
>                         this.ask3D = ask3D;
>                           }
>
>
>                         @Override
>                           protected Void doInBackground() throws
> Exception {
>                           if (ask3D) {
>                                  stopButton.setEnabled(true);
>
>                                  track();//the method which holds
> everything that the trackButton should do!!!
>                                 }
>
>                           else {
>                                 stopButton.setEnabled(true);
>
>                                 }
>
>                           return null;
>                         }
>                    }
> ///lot of code after not shown......
>
>
> Best Regards,
>
> Sarala
>
>
>
> _______________________________________________
> micro-manager-general mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/micro-manager-general



_______________________________________________
micro-manager-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/micro-manager-general
Reply | Threaded
Open this post in threaded view
|

Re: swingworker.cancel() is giving errors. Attempting to use “stop button” to stop the code from running - MM1.4

SaralaT

Hi Nico,


Thanks a lot for your help. I will take a look at how I can modify the code.




Best Regards,

Sarala


From: Stuurman, Nico <[hidden email]>
Sent: Friday, January 11, 2019 2:30:26 AM
To: Micro-Manager General
Subject: Re: [micro-manager-general] swingworker.cancel() is giving errors. Attempting to use “stop button” to stop the code from running - MM1.4
 
Hi Sarala,

This is more a general Java question, probably best asked on
StackOverflow or similar.  However, I would have a look at:
https://docs.oracle.com/javase/tutorial/uiswing/concurrency/cancel.html.
It looks to me that your call "trackSPTInstant.cancel(true);", results
in an interrupt in your Swingworker that is not handles (caught) by your
SwingWorker (although it is confusing that you do not get an
"Interrupted" exception).  You could either try to catch the Interrupt
in your SwingWorker, or call " trackSPTInstant.cancel(false);", and
check SwingWorker.isCancelled regularly in your code.

Hope this helps!

Best,

Nico



> Hi everyone,
>
>
> I have a problem with using the swingworker to add a functional stop
> button to a set of code. Once pressed I'd like the code to stop and
> exit. How ever, I am new to the swingworker itself, and the stop
> button I placed seems to give me a Java error in a micromanager error
> window. My code snippet is as below (as I am relatively new to Java
> programming, please excuse my untidy and perhaps not so efficient code
> set). Here once the program starts running, it successfully prints out
> "TRACKSPTINSTANT EXECUTE - OK", and goes on to do what is indicated
> inside the  track(). While it is running, when I press "stop button",
> how ever, it gives the error as below, and then once I press ok , it
> just goes on to print "TRACKSPTINSTANT CANCEL - OK" and continues with
> the rest of the code, without stopping. I am unsure of why this
> happens. Would be great if some one could help me with some advise on
> how I may go ahead to correct this problem. What does this error mean?
> Is there a more simple way of adding a stop button to do this same
> function that I am trying to do?
>
>
> Thanks a lot in advance!
>
>
>
>
>
>
> ////lot of code before not shown.....
> ActionListener trackButtonPressed = new ActionListener() {
>                 @Override
>                 public void actionPerformed(ActionEvent event){
>                         boolean ask3D = true;
>
>                         trackSPT(ask3D);
>                         }
>                  };
>
> public void trackSPT(boolean ask3D) {
>                 trackSPTInstant = new trackSPTWorker(ask3D);
>                 trackSPTInstant.execute();
>                 IJ.log("TRACKSPTINSTANT EXECUTE - OK");
>                 }
>
> ActionListener stopButtonPressed = new ActionListener() {
>                 @Override
>                 public void actionPerformed(ActionEvent event){
>                        trackSPTInstant.cancel(true);//this is somehow
> not working... why.....
>                 IJ.log("TRACKSPTINSTANT CANCEL - OK");
>                 }
>                 };
>
> public class trackSPTWorker extends SwingWorker<Void, Void> {
>                         private final boolean ask3D;
>                         public trackSPTWorker(final boolean ask3D) {
>                         this.ask3D = ask3D;
>                           }
>
>
>                         @Override
>                           protected Void doInBackground() throws
> Exception {
>                           if (ask3D) {
>                                  stopButton.setEnabled(true);
>
>                                  track();//the method which holds
> everything that the trackButton should do!!!
>                                 }
>
>                           else {
>                                 stopButton.setEnabled(true);
>
>                                 }
>
>                           return null;
>                         }
>                    }
> ///lot of code after not shown......
>
>
> Best Regards,
>
> Sarala
>
>
>
> _______________________________________________
> micro-manager-general mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/micro-manager-general



_______________________________________________
micro-manager-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/micro-manager-general


_______________________________________________
micro-manager-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/micro-manager-general