dm 脚本捕获错误 "Analyze Particles"

标签 dm-script

做的时候

ChooseMenuItem("Analysis", "Particles", "Analyze Particles") 

我有时会收到“无效索引”错误窗口。有没有办法捕获该错误? 这样做,

try {
    ChooseMenuItem( "Analysis", "Particles", "Analyze Particles" )
}
catch {
    okdialog("error")
}

没有捕捉到错误。 “无效索引”错误可能是在完成菜单操作“分析粒子”后出现的错误。谁能指出如何捕捉这个错误?找出这个错误的根源是一个很大的优势。我使用的是 GMS 1.84。

最佳答案

I think the problem you're encountering is that the Particle-Analysis is running (at least partly) on a separate background-thread.

I don't believe there is a way to directly catch these exceptions in this case.

我不再使用 GMS 1.84,但我确实在 GMS 3.2 上进行了尝试,您可能也想这样做以更好地了解发生了什么。


首先,您的 Try/Catch 循环是可以的,但是如果您不在 catch 中放置“break”,那么一旦离开 catch 部分,异常仍然会提升到系统,即您经常想做的事:

Try{ 
    ... }
Catch{
    ...
    break
}
...

为了测试脚本对被调用方法的异常的行为方式,我首先编写了一个小脚本并将其“安装”为菜单命令,一次使用后台线程,一次不使用后台线程。我通过自定义菜单中的文件菜单安装了它们,命令名称分别为BTnBT:

// $BACKGROUND$
Result( "\nStart and wait" )
number i = 0
while( i < 100 ){
    i++
    sleep(0.05)
    if ( ShiftDown() ) break
    if ( OptionDown() ) Throw("Broken")
    Result( "." )
}
Result("\nDone and exit.")

Result( "\nStart and wait" )
number i = 0
while( i < 100 ){
    i++
    sleep(0.05)
    if ( ShiftDown() ) break
    if ( OptionDown() ) Throw("Broken")
    Result( "." )
}
Result("\nDone and exit.")

然后我使用“ChooseMenuItem()”在以下脚本中进行测试:

string name = TwoButtonDialog("Background threaded?", "yes", "no" ) ? "BT" : "nBT"
number success = 0
Try{
    Result( "\n Calling: " + name )
    success = ChooseMenuItem("Custom","",name)
}
catch
{
    Result("\n Caught exception." )
    break
}
result("\n Success: " + success )

使用此组合进行测试(并使用 ALT 键在例程中引发异常),我可以验证命令的行为符合预期:

  • If the routine started by the ChooseMenuItem command is launched on the main-thread, then the execution of that call 'blocks' the main script until it is completed - either at its end, or when it throws and exception. The main script correctly catches exceptions and prints the result.
  • If the routine started by the ChooseMenuItem command is launched on a separate (background) thread, then the main-script continues immediately. ChooseMenuItem returns successfully at once (if it could launch the command), and the Try/Catch loop is exited. Any exception thrown by the called routine on the background thread will not be caught anymore.

As for the origin of the error: The "Invalid index" message points to some object being removed (or kept in scope) by the main-script which is expected to be there (or no longer there) by the called background routine. This could be an image or imageDocument or the display of an image or any object (ROI, mask...) on an imageDisplay.

我怀疑你的主脚本正在做一些事情,比如关闭使用过的图像?如果“分析”是在单独的线程上进行的,则您的主脚本可能会太快或太慢,从而导致事情不同步。您可能需要在主脚本中添加人工暂停 (sleep()) 和更复杂的图像跟踪系统(使用image-ID)来避免这种情况事物。

使用ChooseMenuItem()是一种变通的黑客解决方案,因此针对您的问题的任何错误预防解决方案也可能是一种代码黑客,需要一些丑陋的“创造力”。

关于dm 脚本捕获错误 "Analyze Particles",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48123649/

相关文章:

dm-script - 如何移动 SI 图像中的列

dm-script - CountImages() 实际上做了什么?

dm-script - 设置实场输入小部件的阈值

dm-script - 如何使用DM脚本读取PC信息

image-processing - 将 STEM 数据立方体乘以图像

dm-script - 是否可以通过命令生成gtk

type-conversion - 有更好的方法将浮点图像转换为整数图像吗?