szieke / ScriptCommunicator_serial-terminal

Scriptable cross-platform data terminal which supports: serial port, UDP, TCP, SPI, I2C and CAN.

Home Page:https://sourceforge.net/projects/scriptcommunicator/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Question: intended behavior scriptThread.stopScript()

flo80 opened this issue · comments

commented

When developing a script today, I wanted to stop execution when the user cancels a selection in a combo box.
I called scriptThread.stopScript() but the execution did not stop (ran into an exception since the lookup of the non existent / empty selected value failed)

What is the intended way to completely stop the execution of a script? Is this possible at all? Or do I need to build a big chain of if/else to make sure I do not run into issues with not selected values?

scriptThread.stopScript() is the function to stop a script. Maybe you can upload your script and I can debug it.

commented

Here a simple example

const list = ["a", "b", "c"]

function getListItem() {
  const input_string = scriptThread.showGetItemDialog("Select", "test", list)
  if (input_string == "") {
    scriptThread.messageBox("Error", "Select", "Required")
    scriptThread.stopScript()
    return null
  }

  return list.indexOf(input_string)
}

var t = getListItem()
scriptThread.messageBox("Info", "Selected", t.toString())

Note: when I am not using a function the script also doesn't stop immediately (in this case it would show index -1 and not have an exception)

Screen.Recording.2022-07-25.at.09.00.45.mov

The reason for the exception is return null. null is an undefined object. If you call a function (toString()) from an undefined object an exception is thrown. If you change null for example to -1 (number object) then the exception does not occur.

commented

To be honest, I thought stopScript would exit the script directly because it calls the exit function of the thread which executes the script (every script is executes in its own thread). But the code that is not in a slot function (slot functions are called if a signal is emitted (e.g. from a timer or data reception)) is executed by the script interpreter before the exec function of the thread is called. In other words, if you call stop script in code that is not in a slot function then the script does stop if the end of the script is reached, if called in a slot function the script stops immediately.
I will change this behavior in the next release (if I cannot change it then I will describe it in the documentation).

Hi,
I fixed this issue (on the master branch). If a script calls stopScript then the execution is stopped immediately. I will create a new release next week.
Sorry for fixing this so late, I was really busy the last months.