Question

I am trying to launch a background thread in the start of my javaFX application, but my UserInterface(UI) freezes nevertheless. this is what i am doing :

Task task = new Task<Void>() {
    @Override protected Void call() throws Exception {         
            Platform.runLater(new Runnable() {
        @Override
        public void run() {
            try {

                Class.forName("org.sqlite.JDBC");
                Connection c = DriverManager.getConnection("jdbc:sqlite:visuAgent.db");
                c.setAutoCommit(false);
                Statement stmtt = c.createStatement();

                ResultSet rs = stmtt.executeQuery( "select max(num_step) from avoir_continent " );               
                rs.close();
                stmtt.close();

            } catch (ClassNotFoundException ex) {
                Logger.getLogger(rafraichirbd.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SQLException ex) {
                Logger.getLogger(rafraichirbd.class.getName()).log(Level.SEVERE, null, ex);
            }        
        }
   });       
        return null;
    }
};  

                Thread th = new Thread(task);
                th.setDaemon(true);
                th.start(); 

Any idea ?thank you .

Was it helpful?

Solution

You are using Platform.runLater() inside your Task's call(). The Platform.runLater() takes you back to the UI thread.

Try removing it, while doing the computation and use it only when you need to update some data on the UI

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top