0

I'm making a program which sends some data to the Matlab and receives output from matlab. To do that, I used matlabcontrol library.

Problem

After I click Java FX application button to submit data, matlabcontrol opens Matlab for further calculations. But when java opens the matlab, Java FX application stuck with wait cursor.Then starts to work again after Matlab finishes the process of calculation.

What I did

public void runner()
{
    Platform.runLater(new Runnable()
    {
        @Override
        public void run()
        {
            firstFunc();
        }
    });

    Platform.runLater(new Runnable()
    {
        @Override
        public void run()
        {
            secondFunc();
        }                
    }); 
}

public void firstFunc()
{
// This function controls UI while Matlab does it's calculations
    double progress = 0.2;
    progressLabel.setText(progress*100+"% Completed");
    progressBar.setProgress(progress);

}

public void secondFunc()
{
// This method creates matlab connection and handle matlab
    firstClass mainFunc = new firstClass(pathSelected);
    mainFunc.func();
}

So I used Platform runLater to run two methods separately. But still my program stuck with wait cursor when Matlab starts to functioning. I also used threads to run these functions in parallel. But had the same issue. How can I correct this. Any help?

Update

As described in this question, I did use service and task with countdownlatch. But still didn't get what I wanted. In there,

Service<Void> service = new Service<Void>() {
    @Override
    protected Task<Void> createTask() {
        return new Task<Void>() {           
            @Override
            protected Void call() throws Exception {
                //Background work                       
                final CountDownLatch latch = new CountDownLatch(1);
                Platform.runLater(new Runnable() {                          
                    @Override
                    public void run() {
                        try{
                            //FX Stuff done here
                            firstFunc();
                        }finally{
                            latch.countDown();
                        }
                    }
                });
                latch.await();                      
                //Keep with the background work
                // I added matlab calling function here.
                secondFunc();
                return null;
            }
        };
    }
};
service.start();

latch await and let background work to carry on. But in my case, my FX application shows a progress bar. So it should always update while background task happens. In here, it finishes FX task and moves to background task. I didn't get what I wanted. please help.

sam1234
  • 33
  • 1
  • 8
  • 1
    Don't run `secondFunc()` on the JavaFX application thread, i.e. remove the `Platform.runLater` for this method call and instead make sure it is not run on the JavaFX application thread. To clarify, `Platform.runLater` will run the `Runnable` on the GUI thread (JavaFX Application thread), not a separate background thread. – d.j.brown Apr 07 '18 at 18:16
  • @d.j.brown I did update my question. Please help. – sam1234 Apr 07 '18 at 18:47

1 Answers1

0

I had to use Service, Task and CountDownLatch to accomplish this task as I mentioned in Question Update part.

Also, I had to run another task inside the firstFunc, where I did update the progress. Like in this answer.

sam1234
  • 33
  • 1
  • 8