Task of script in Factory studio

Dear experts
In my project, I had 2 task script called main and Stop with corresponding to trigger as below picture


I have 2 cases

  1. These task can run at the same time or not?
  2. When Task main is running, I want to use task stop_emercency to stop main Task. Is that Possible?
    Please help me this articles
    Best

Hi Manh,

About 1 - yes, Tasks can run at the same time, they run in different Threads.

About 2, we are still checking with the developers the best way to do that.

Bests,
Tatsoft Team.

Dear expert.
Thank for your response
Best

Hi Manh,

About 2 - Works for you to use like this? You can use the trigger from stop to exit the main code:
image

Bests,
Tatsoft Team.

Dear expert
Thank for your support
The main script have command line. If I use return key, I have to use it many times in main script.
Is there any way to stop main script from other script?
Best

Hi Manh,

To ensure the integrity of the system, the .NET Framework does not provide a direct way to stop a thread. Instead, threads should be programmed to periodically check a condition that indicates whether execution should be interrupted, such as a control variable or an event. When the condition is met, the thread can exit the loop or terminate execution appropriately.

You can use as an example the code below, it will check periodically if the trigger tag is equal to 1, if not , the thread will keep running.

image

Did the previous response answer helps you?

Bests,
Tatsoft Team.

Dear expert
Thank for your support.
I tried to use “exit” in my code. But the error happened.

How can I fix this?
Best

Hi Manh,

We noticed that you are facing an error which may be due to the absence of a method called “exit”. You said you want to avoid writing repetitive code, so we suggest reusing code using methods, and we found a helpful link at:

Also, if you want to create methods you should create a Class, and this Task calls the Class, because only Classes can have methods, more information here:
https://docs.tatsoft.com/display/DOC91/Script+-+Tasks+and+Classes

Bests,
Tatsoft Team.

Dear expert
I also tried it in class script. It have a same error

Hi Manh,

This is a C# code that defines a function called Main(). The Main() function contains a loop that will run five times, from i = 1 to i < 6. Within the loop, there is a conditional statement that checks whether the exit() function returns true when passed the current value of i.

The exit() function takes an integer argument i and returns a boolean value based on whether i is equal to 3. If i is equal to 3, the function returns true; otherwise, it returns false.

If the exit() function returns true for the current value of i, the return statement inside the conditional statement will immediately exit the Main() function, causing the loop to stop executing and the program to terminate.

public void Main()
{
    for (int i = 1; i < 6; i++)
    {
        //Put your code here

        if (exit(i))
        {
            return;
        }
    }
}

public bool exit(int i)
{
    return i == 3;
}

Please inform us if you have comprehended this explanation. If you require further clarification or additional examples, do not hesitate to let us know, and we will gladly provide them.

Bests,
Tatsoft Team.