Forum Home › Forums › Understanding the Software › Using Formulas › Faster thread
Tagged: Faster thread
- This topic has 18 replies, 3 voices, and was last updated 1 month, 2 weeks ago by
Mikhail.
-
AuthorPosts
-
July 9, 2025 at 7:13 am #16618
詹森
ParticipantIn the script calculation, the smallest execution interval currently observed is 1 second (using EverySec(getDataFunc), a method can be called once per second). I would like to ask if there are faster threads and methods? For instance, if I want my script to perform a calculation every 100 milliseconds or even 10 milliseconds, how can I achieve this? Want to achieve faster threads,Thank you for your help
| Function | Data Type | Description |
| ——————— | ——— | ——————————————– |
| EverySec(getDataFunc) | CnlData | Executes the specified function every second |July 9, 2025 at 7:29 am #16619manjey73
ParticipantAll formulas in the channels are executed in a cycle from the smaller channel to the larger one. If you don’t specify every second, then it’s about 10 times per second. And the more channels there are, the longer the pause time will be.
Or don’t use the every second wrapper, or use your own timer for example.July 9, 2025 at 7:54 am #16620詹森
ParticipantHow can one implement their own timer? How to create a custom thread? Could you please explain the implementation steps or examples? Thank you very much for your help!!
July 9, 2025 at 8:07 am #16621manjey73
ParticipantWhat’s wrong with processing a script in a loop, which also requires a timer of less than a second?
To be honest, I haven’t implemented timers for such a task. I usually use them for other purposes, for example, to delay triggering the device status.
What exactly is your goal? Can you draw a flowchart for understanding?An example of a TON timer from CodeSys or similar.
// IEC timers and others public static long Ticks() { DateTime now = DateTime.Now; long time = now.Ticks/10000; return time; } protected class UtilTimer { public long et; public bool flag; public bool q; } protected Dictionary<string, UtilTimer> TonTimer = new Dictionary<string, UtilTimer>(); protected Dictionary<int, UtilTimer> TofTimer = new Dictionary<int, UtilTimer>(); protected Dictionary<int, UtilTimer> TpTimer = new Dictionary<int, UtilTimer>(); // Timer with a delay of turning on TON public double TON(double IN, double PT, double Q, string str = "", int idx = 0) { var ut = new UtilTimer() {et = 0L, flag = false, q = false}; string keys = $"{CnlNum}_{idx}"; // The key is the channel number plus the timer index, zero by default. Allows you to use multiple timers inside a formula. long ET = 0L; long _pt = Convert.ToInt64(PT); bool q = Q > 0; bool _in = IN > 0; string s = str.ToLower(); if (s == "s" || s == "sec") _pt = Convert.ToInt64(PT) * 1000; if (s == "m" || s == "min") _pt = Convert.ToInt64(PT) * 60000; if (s == "h" || s == "hour") _pt = Convert.ToInt64(PT) * 3600000; if (!TonTimer.ContainsKey(keys)) { TonTimer.Add(keys, ut); } if (!_in) { TonTimer[keys].q = false; TonTimer[keys].flag = false; TonTimer[keys].et = 0L; } else { if (!TonTimer[keys].flag) { TonTimer[keys].flag = true; TonTimer[keys].et = Ticks(); } else { if (!q) ET = Ticks() - TonTimer[keys].et; } if (ET >= _pt) q = true; TonTimer[keys].q = q; } q = TonTimer[keys].q; return Convert.ToDouble(q); } // -------------------------------------------- // Timer with delayed shutdown of TOF public double TOF(double IN, double PT) { var ut = new UtilTimer() {et = 0L, flag = false}; long ET = 0L; long _pt = Convert.ToInt64(PT); bool q = Val(CnlNum) > 0; bool _in = IN > 0; if (!TofTimer.ContainsKey(CnlNum)) { TofTimer.Add(CnlNum, ut); } if (_in) { q = true; TofTimer[CnlNum].flag = true; TofTimer[CnlNum].et = 0L; ET = 0L; } else { if (TofTimer[CnlNum].flag) { TofTimer[CnlNum].flag = false; TofTimer[CnlNum].et = Ticks(); ET = 0L; } else { if (q) ET = Ticks() - TofTimer[CnlNum].et; } if (ET >= _pt) q = false; } return Convert.ToDouble(q); } // -------------------------------------------- // Pulse Timer TP public double TP(double IN, double PT) { var ut = new UtilTimer() {et = 0L, flag = false}; long ET = 0L; long _pt = Convert.ToInt64(PT); bool q = Val(CnlNum) > 0; bool _in = IN > 0; if (!TpTimer.ContainsKey(CnlNum)) { TpTimer.Add(CnlNum, ut); } if (!TpTimer[CnlNum].flag) { if (_in) { TpTimer[CnlNum].flag = true; TpTimer[CnlNum].et = Ticks(); if (ET < _pt) q = true; } } else { if (q) { ET = Ticks() - TpTimer[CnlNum].et; if (ET >= _pt) q = false; } else { if(!_in) { TpTimer[CnlNum].flag = false; ET = 0L; } } } return Convert.ToDouble(q); }
July 9, 2025 at 8:08 am #16622manjey73
ParticipantYou can use it inside your scripts. Someone also has to set the timer so that it starts working.
July 9, 2025 at 8:54 am #16623詹森
Participant1. We have encountered a situation where there are many computations in the channel, resulting in the script being called in the channel only calculating once every few seconds. Therefore, this method is currently unavailable.
2. Using “EverySec (getDataFunc)” can only calculate once per second at the fastestI currently have a requirement to perform a script calculation every 100 milliseconds to obtain the latest calculation results in real time. The main need is to meet some calculations that require rapid response. Could you please inform me if there are any other methods to meet such a requirement? Thank you very much for your reply. Thank you
July 9, 2025 at 9:03 am #16624manjey73
Participant1. How does your script run for a few seconds without using EverySec?
How did you determine that?July 9, 2025 at 11:33 am #16626詹森
ParticipantDue to the large number of data channels, I found that the script directly called in the channel has slowed down to about 10 seconds before calling the calculation once. If I want to call the calculation script once every 100 milliseconds, how can I achieve this? Could you please let me know? Thank you
July 9, 2025 at 1:06 pm #16627manjey73
ParticipantI don’t understand how your script works fast when using the limit of once per second (everysecond) and at the same time runs for 10 seconds with a direct call?
Show your script that behaves this way.
July 9, 2025 at 1:23 pm #16628詹森
ParticipantMethod 1: Call the script in the channel. It takes only 10 seconds to poll for one cycle (why is it so long because there are many computing channels).
Method 2: Use EverySec (getDataFunc), but with one call cycle per second
I’d like to ask if there are other ways to call a script (method, function) once every 100 milliseconds?
Thank you for your help
July 9, 2025 at 1:40 pm #16629manjey73
ParticipantPlease show me your script.
When calling the script directly, it should run faster than when called through EverySec.July 9, 2025 at 1:49 pm #16632Mikhail
ModeratorHello,
The main loop of the Server service iterates approximately 10 times a second. If you need faster calculation rate, I suggest to develop a specific module in C#.July 9, 2025 at 1:58 pm #16633manjey73
Participantso 10 times per second is about a 100ms cycle 🙂
July 9, 2025 at 2:05 pm #16634詹森
ParticipantHello, regarding the #16629 issue of manjey73, since I use a large number of channels, there are approximately 5,000 computing channels. This has led to a polling cycle of calculation taking up to 10 seconds. This is related to CPU performance. Is there any solution? Thank you very much for your help.
Hello Mikhail, you mentioned that specific modules can be developed. Do you have any relevant materials or examples for this? Please help me when it’s convenient. Thank you very much for your assistance.
July 10, 2025 at 5:06 am #16635manjey73
ParticipantYou’ve voiced the problem, but you haven’t provided a single script that you think could lead to this.
-
AuthorPosts
- You must be logged in to reply to this topic.