PowerApps中的事件处理和线程安全(Event Handling and Thread Safety in PowerApps)

编程入门 行业动态 更新时间:2024-10-09 19:14:56
PowerApps中的事件处理和线程安全(Event Handling and Thread Safety in PowerApps)

除用户事件外,Powerapps还支持计时器事件。 这引入了并发计时器访问全局变量或集合的可能性。 文档不提供有关事件处理的信息(并发与顺序,消息泵与回调)。 Powerapps社区论坛是沉默的。 我在这里发帖询问SO社区是否在重新发明轮子之前已经对此进行了测试。 (SO搜索显示没有相关主题,如果有的话会很乐意更正)。

示例用例是使用定时器分离和后台条形码注册和数据传输。 即想象一家杂货店退房。

如果没有现有信息,将报告调查结果。

Powerapps supports timer events in addition to user events. This introduces the possibility of concurrent timers accessing global variables or collections. Documentation provides no information on event handling (concurrent vs sequential, message pump vs callback). Powerapps community forum is silent. Am posting here to inquire with the SO community whether testing has been done on this already before re-inventing the wheel. (SO search revealed no relevant topics, would happily stand corrected if there are).

Example use case is separating and backgrounding barcode registration and data transmission using timers. I.e. imagine a grocery store check out.

Will report back on findings if no existing information available.

最满意答案

PowerApps构建于JavaScript之上,基于回调,它是单线程(*)。 每个表达式都作为单个“执行单元”执行,所以如果你有类似的东西:

UpdateContext({ a: 1, b: 2 }); UpdateContext({ c: a + b })

您可以指望“a”和“b”将一起初始化(不会被中断)。 但是,你不能依赖于第二个UpdateContext调用将在任何可能改变'a'或'b'值的事件之前进行的事实(例如计时器的OnTimerEnd表达式) - 尽管这种情况不太可能发生。

对于计时器的具体示例,如果您有两个带有此表达式的计时器:

Set(globalVarA, globalVarA + 1)

有可能(尽管不太可能,特别是对于这种简单的操作),在调用Set函数的内部实现以更新该变量的值之前,将计算两个定时器的'globalVarA + 1'的值(in在哪种情况下,应用程序将“丢失”其中一个增量)。

当您调用数据源(涉及较慢的网络调用)时,更可能发生问题的情况。 例如,如果您在两个计时器上有此表达式:

Set(oldVar1, var1); Patch(dataSource, record, changes); Set(var1, oldVar1 + 1)

然后,竞争条件发生的可能性增加了很多。

希望这可以帮助!

(*)目前JS工作线程的使用非常有限,因此应用程序在技术上并不是完全单线程的,但这不应该影响您编写的表达式。

PowerApps is built on JavaScript, which is single threaded (*), based on callbacks. Every expression is executed as a single "unit of execution", so if you have something like:

UpdateContext({ a: 1, b: 2 }); UpdateContext({ c: a + b })

You can count on the fact that the 'a' and 'b' will be initialized together (without being interrupted). You cannot, however, rely on the fact that the second UpdateContext call will be made before anything happens that may change the values of 'a' or 'b' (such as a timer's OnTimerEnd expression) - although it's very unlikely that this will happen.

For the specific example of timers, if you have two of them with this expression:

Set(globalVarA, globalVarA + 1)

It's possible (although quite unlikely, especially for this kind of simple operation) that the value of 'globalVarA + 1' will be calculated for the two timers before the internal implementation of the Set function is called to update the value of that variable (in which case the app would "lose" one of the increments).

A scenario where a problem is more likely to happen is when you have calls to data sources (which involve network calls that are slower). For example, if you have this expression on two timers:

Set(oldVar1, var1); Patch(dataSource, record, changes); Set(var1, oldVar1 + 1)

Then the likelihood of a race condition happening increases by a lot.

Hope this helps!

(*) Currently there is some very limited usage of JS worker threads, so the app isn't technically completely single threaded, but that should not affect expressions that you write.

更多推荐

本文发布于:2023-07-29 03:26:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1311964.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:线程   事件   PowerApps   Event   Safety

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!