question

Yue Y avatar image
0 Likes"
Yue Y asked Phil BoBo commented

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

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?

FlexSim 19.1.0
webserverthread
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

1 Answer

·
Jacob Gillespie avatar image
1 Like"
Jacob Gillespie answered Jacob Gillespie edited

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.

· 20
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Yue Y avatar image Yue Y commented ·

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

0 Likes 0 ·
Jacob Gillespie avatar image Jacob Gillespie ♦ Yue Y commented ·
0 Likes 0 ·
Yue Y avatar image Yue Y commented ·

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 0 ·
Jacob Gillespie avatar image Jacob Gillespie ♦ Yue Y commented ·

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

0 Likes 0 ·
Yue Y avatar image Yue Y Jacob Gillespie ♦ commented ·

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

0 Likes 0 ·
Show more comments
Show more comments
Yue Y avatar image Yue Y commented ·

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

0 Likes 0 ·
Ben Wilson avatar image Ben Wilson ♦♦ Yue Y commented ·

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 0 ·
Yue Y avatar image Yue Y Ben Wilson ♦♦ commented ·

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

0 Likes 0 ·
Show more comments
Yue Y avatar image Yue Y commented ·

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 0 ·
Phil BoBo avatar image Phil BoBo ♦♦ Yue Y commented ·

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

1 Like 1 ·
Yue Y avatar image Yue Y Phil BoBo ♦♦ commented ·

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

0 Likes 0 ·
Show more comments

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.