Can we create a background thread in FlexSim that do some tasks once a second?

Can we create a background thread in FlexSim that do some tasks once a second?

yue_y
Not applicable
413 Views
21 Replies
Message 1 of 22

Can we create a background thread in FlexSim that do some tasks once a second?

yue_y
Not applicable

[ FlexSim 19.1.0 ]

I want to create a thread and let FlexSim report some data to another server once a second. Is it possible to create a separate thread that does this job?

0 Likes
Accepted solutions (1)
414 Views
21 Replies
Replies (21)
Message 2 of 22

Jacob_Gillespie
Autodesk
Autodesk
Accepted solution

To create a thread you would have to use C++. See this answer for an example of using a thread.

You might be able to do this with the setticker(node thenode[, num tickspersecond]) command.

Message 3 of 22

yue_y
Not applicable

Thank you Jacob, any tutorial on writing C++ in FlexSim?

0 Likes
Message 4 of 22

yue_y
Not applicable

Also, I am not looking for the exact thread concept. Anything with the behavior that executes a FlexScript snippet once a second on the side is enough

0 Likes
Message 5 of 22

Jacob_Gillespie
Autodesk
Autodesk

@Yue Y That's what setticker() does. It executes a flexscript / cpp node x times per second.

0 Likes
Message 6 of 22

yue_y
Not applicable

Is this command executed separately from the the simulation process? Will it block the whole simulation?

0 Likes
Message 7 of 22

Jacob_Gillespie
Autodesk
Autodesk

Yes it will block

0 Likes
Message 8 of 22

yue_y
Not applicable

Will it block between two execution or it will only block during execution?

0 Likes
Message 9 of 22

Jacob_Gillespie
Autodesk
Autodesk

It will block only during execution, not between.

0 Likes
Message 10 of 22

Jacob_Gillespie
Autodesk
Autodesk
0 Likes
Message 11 of 22

yue_y
Not applicable

Could you give an example of using setticker? Especially what does the first parameter look like?

0 Likes
Message 12 of 22

Ben_WilsonADSK
Community Manager
Community Manager

Hi @Yue Y,

Please find attached a sample model showing usage for setticker(). In my testing, it appears that the tickspersecond parameter must be >=1.

Call setticker() after the model run has started - for instance on some object or model trigger, or on a scheduled user event.

setticker-example.fsm

0 Likes
Message 13 of 22

yue_y
Not applicable

Thank you Ben! I am running the example you provided, is there a way to stop this script programmably or manually?

0 Likes
Message 14 of 22

yue_y
Not applicable

I found it in the user manual:

setticker(0) can be used to turn off the execution
0 Likes
Message 15 of 22

yue_y
Not applicable

Hi Ben @Ben Wilson, how did you add the ticketLogic in Tools folder?

0 Likes
Message 16 of 22

Ben_WilsonADSK
Community Manager
Community Manager

Hi @Yue Y,

Here is how I added a new node into the Tools folder (via right-click on the Tools node, but would work for any parent node):

19780-2019-06-10-16-16-21.png

Here is how I added ability to add text to the node:

19781-2019-06-10-16-16-22.png

Here is how I toggled the node to be a FlexScript node:

19782-2019-06-10-16-18-57-flexsim-2019.png

Message 17 of 22

yue_y
Not applicable

The command executes the node for "tickspersecond" times every second of physical time. Is there any similar function that works for simulation time(eg. execute once every 5 simulation time units)? I am considering using physical time x speed in this function but if the simulation is stopped or blocked, this function will still be executed. Any other command that I can use?

0 Likes
Message 18 of 22

philboboADSK
Autodesk
Autodesk

If you want to repeatedly execute a function in simulation time, use a repeating User Event:

19801-userevent-repeat.png



Phil BoBo
Sr. Manager, Software Development
Message 19 of 22

yue_y
Not applicable

Could you please give an example of creating an user event that can execute custom code repeatedly in FlexScript? Thank you!

0 Likes
Message 20 of 22

philboboADSK
Autodesk
Autodesk

I did in the screenshot above. It executes the Event Code repeatedly.

  • Add a user event
  • Check the "Repeat event" box
  • Specify the Repeat Event Time
  • Edit the Event Code

This is a standard toolbox item. You just use it. No example is needed.



Phil BoBo
Sr. Manager, Software Development