Can you set a process timeout

I can succesfully invoke arduino-cli to build and upload programs to an arduino board.
The problem that I am having, is that the upload process takes 2 minutes before a status is returned.

When I select the wrong board type the program AVRDUDE takes 10 upload attempts and it waits a rediculous amount of time in between attemps. The thing is… if the first attempt fails, all attempts will.

What I want is a timeout on the process. If the process does not return in… lets say 10 seconds, I want to kill the process and display that the upload process has failed and the program should move on. As my code is now, my entire application freezes for 2 minutes.

I tried the function waitFor() but it does not seem to work or do what I want…

        if( status ==  true )
        {   status  = false ;

            println("SUCCES") ;
            print("\r\nUPLOADING . . . ") ;

            command = arduinoCliPath + uploadCommand ;
            p = launch(command);
            if(!p.waitFor(10, TimeUnit.SECONDS)) { p.destroy();  }

            in = new BufferedReader(new InputStreamReader( p.getInputStream())); // extra debug stuff
            while ((line = in.readLine(  )) != null)
            {
                if( line.contains("New upload port"))
                {
                    status = true ;
                }
            }
            if( status == true )
            {
                println("SUCCES") ;
            }
            else
            {
                println("FAILED") ;
            }
        }
        else
        {
            println("FAILED") ;
        }
    } 
    catch (RuntimeException e) {println("RuntimeException, it fails") ; }
    catch (IOException e) {println("IOException, it fails") ; }
    catch (InterruptedException e) {println("FAILED") ; }

Can I implement a timeout and ifso how should I do that?

Kind regards :tumbler_glass:

Bas

Well I’d like to know how to start a process on the background and generate a timeout if the process does not complete in a certain amount of time.

Hello @bask185 ,

Consider using these:

:)