Subscribe to this thread
Home - General / All posts - Running a script from another script in an M9 project
gjsa100 post(s)
#23-Oct-20 00:45

Hi, I have a VBScript in my project and I want it to start another independent VBScript under certain conditions.

What is the sequence of commands needed in the first VBScript to call/run another script defined in the M9 project ?

So far I have found this:

http://www.georeference.org/forum/t148566.9#148604

... but I can't adapt that approach (which triggers the secondary script via a query) to run the script as a whole (as it would run if I right-clicked on the script and selected 'run'). Note that the second script contains a main sub procedure and an auxillary sub procedure that is reqd by main.

I guess you could say, why don't I just put the procedures in the second script into the first script and call/run it that way? Because I want to call the procedure in that script sometimes from within the first script and at other times, independently.

gjsa100 post(s)
#25-Oct-20 21:50

One obvious solution is to duplicate the code in sub procedures required into both scripts. This is what I'm doing, it just means I need to be careful if I modify the procedure (bug fix, improvements) in once script to remember to include those modifications in the second.

adamw


10,447 post(s)
#02-Dec-20 15:05

We can allow calling a script from a different script in the API.

We hesitate doing that because pausing the first script for the duration of the second is actually not easy. Our scripts can spawn threads, they will all have to be paused / resumed, this has to be coordinated. Plus the longer the pause is, the more potential trouble it is, because who knows how much the state of the data will deviate from what it was at the moment of the pause - if it deviates enough, resuming might fail spectacularly. Plus the second script will then be able to invoke a third script, this all is going to be a stack. Long story short, we hesitate allowing scripts to call other scripts because, if done properly, this is fairly complex, so we'd rather try other things first.

From your description, might it be that you will be fine with means to include a script into another script? Ie, you'd write script 1 with some utility functions and without Sub Main, then you'd write script 2 and write something like:

'VBScript

'

' @include [script 1]

 

Sub Main

  ...

End Sub

...and that would allow you to call functions or subs that reside in script 1 from your code in script 2.

Is this essentially what you are looking for?

gjsa100 post(s)
#07-May-21 07:35

At the risk of being too late in responding, yes that's exactly what I need.

What I want to do is simply define sub-procedures once for different script components that need to use them.

Script components that each contain their own Sub Main would only ever run in isolation - so the risk of threads being used unsafely is not present.

Is this actually possible with something like what you paraphrased above and I have clarified below?

'VBScript

'

' @include [sub procedures script]

 

Sub Main

  ...

End Sub

adamw


10,447 post(s)
#25-May-21 13:21

Not yet, but we will add it. We have this for queries, but not for scripts.

tjhb
10,094 post(s)
#26-Oct-20 02:11

I would say: don't use VBScript.

That language severely limits you, in this way and many others.

(In a nutshell, you normally can't start another VBScript script from within VBScript, and if you did manage to do it, you would have no control whatsoever over which script (lines/commands) would execute first. Absolutely a language limitation. That is not what VBScript is for. It is inherently not thread-safe.)

Use a .NET language (any of them), then it's easy.

gjsa100 post(s)
#26-Oct-20 04:12

Thanks - fearless advice. I guess I have been putting off the inevitable - just not relishing translating ~1500 lines of VBScript in M9 to a .NET alternative.

tjhb
10,094 post(s)
#26-Oct-20 04:56

Good.

You know where to go if you get stuck. Or else, here.

KlausDE

6,410 post(s)
#02-Dec-20 13:30

Use a .NET language (any of them), then it's easy.

I'm missing the obvious here. Trying to accomplish just that in C#

I call a function in another script component in Mfd 9 using syntax

// C#

...

c_end = Processing.processAlongEdge(app, ... );

with the function processAlongEdge() in class Processing in script [Processing]:

// C#

...

namespace BewirtschaftungsTool // (same namespace as calling script)

{

    public class Processing

    {

...

 public static double processAlongEdge(Manifold.Application app, ...)

 {

 ...;

 }

But I get error message (free translated) Name 'Processing' not present in current context


Do you really want to ruin economy only to save the planet?

adamw


10,447 post(s)
#02-Dec-20 15:08

I believe Tim meant compiling the helper script into a DLL. This currently means getting out of 9 (into, say, Visual Studio). The compiled DLL can then be referenced in a script via @reference.

KlausDE

6,410 post(s)
#02-Dec-20 15:32

Thx Adam. So that's for the productive version and I'll live with one growing file during development where I choose different versions of code just by commenting out.


Do you really want to ruin economy only to save the planet?

Manifold User Community Use Agreement Copyright (C) 2007-2021 Manifold Software Limited. All rights reserved.