英文:
TaskContinuationOptions combinations
问题
当我查看异步模式PipeTo for Akka.NET时,我发现作者在示例中使用了TaskContinuationOptions和运算符&。这是一个错误还是使用&与Akka.NET和PipeTo的正确方式?
更好地解释一下:
AttachedToParent & ExecuteSynchronously返回0,内部Lambda将被异步调用。
TaskContinuationOptions.cs
>/// <summary>如果未指定任何继续选项,则指定在执行继续时应使用默认行为。无论前置任务何时完成,继续都会异步运行,而不考虑前置任务的最终<see cref="P:System.Threading.Tasks.Task.Status" />属性值。如果继续是子任务,则将其创建为独立的嵌套任务。</summary>
None = 0,
AttachedToParent = 4,
ExecuteSynchronously = 524288, // 0x00080000
问题应该使用"&"还是"|"运算符?
英文:
When I was looked at asynchronous pattern PipeTo for Akka.NET I found example where author uses TaskContinuationOptions and operator &. Is it an erorr or may be it is a propper way to use '&' with Akka.NET and a PipeTo?
For better explain:
AttachedToParent & ExecuteSynchronously gave 0 and the inner lambda would be invoked as asyncronosly task.
TaskContinuationOptions.cs
>/// <summary>When no continuation options are specified, specifies that default behavior
should be used when executing a continuation. The continuation runs asynchronously when the antecedent task completes, regardless of the antecedent's final <see cref="P:System.Threading.Tasks.Task.Status" /> property value. It the continuation is a child task, it is created as a detached nested task.</summary>
None = 0,
AttachedToParent = 4,
ExecuteSynchronously = 524288, // 0x00080000
The question should be there "&" or "|" operator?
答案1
得分: 4
TL;DR:
是的。作者应该使用|
而不是&
。
LONG ANSWER:
按位与 => 仅当比较的两位都为1时,结果位才为1。
按位或 => 仅当两个比较的位中任何一个为1时,结果位才为1。
因此,您首先想要将数字转换为二进制(我会添加一些0以使比较更容易):
- 000000:00000000000000000000(
None
) - 000001:00000000000000000001(
PreferFairness
) - 000002:00000000000000000010(
LongRunning
) - 000004:00000000000000000100(
AttachedToParent
) - 065536:00010000000000000000(
NotOnRanToCompletion
) - 131072:00100000000000000000(
NotOnFaulted
) - 196608:00110000000000000000(
OnlyOnCanceled
) - 262144:01000000000000000000(
NotOnCanceled
) - 327680:01010000000000000000(
OnlyOnFaulted
) - 393216:01100000000000000000(
OnlyOnFaulted
) - 524288:10000000000000000000(
ExecuteSynchronously
)
现在您知道,例如,OnlyOnCanceled
等同于 NotOnFaulted
+ NotOnRanToCompletion
。
或者,使用按位运算符:NotOnFaulted | NotOnRanToCompletion
。
另一方面,NotOnFaulted & NotOnRanToCompletion
等于 0
,对应于 None
。
而 OnlyOnCanceled & NotOnFaulted == NotOnRanToCompletion
。
所以答案是:当您想要组合时,使用 |
。当您想要获取差异时,使用 &
。
希望这个示例使问题更清晰。
英文:
TL;DR:
Yes. The author should have used |
instead of &
.
LONG ANSWER:
Bitwise AND => The resulting bit is 1 only if both compared bits are 1.
Bitwise OR => The resulting bit is 1 if any of the two compared bits is 1.
So you first want to translate the numbers to binary (I'll add some 0's to make the comparison easier):
- 000000 : 00000000000000000000 (
None
) - 000001 : 00000000000000000001 (
PreferFairness
) - 000002 : 00000000000000000010 (
LongRunning
) - 000004 : 00000000000000000100 (
AttachedToParent
) - 065536 : 00010000000000000000 (
NotOnRanToCompletion
) - 131072 : 00100000000000000000 (
NotOnFaulted
) - 196608 : 00110000000000000000 (
OnlyOnCanceled
) - 262144 : 01000000000000000000 (
NotOnCanceled
) - 327680 : 01010000000000000000 (
OnlyOnFaulted
) - 393216 : 01100000000000000000 (
OnlyOnFaulted
) - 524288 : 10000000000000000000 (
ExecuteSynchronously
)
Now you know, for example, that OnlyOnCanceled
is the same as NotOnFaulted
+ NotOnRanToCompletion
.
Or, using bitwise operators: NotOnFaulted | NotOnRanToCompletion
.
On the other hand NotOnFaulted & NotOnRanToCompletion
is equal to 0
, that corresponds to None
.
While OnlyOnCanceled & NotOnFaulted == NotOnRanToCompletion
.
So the answer is: when you want to combine, use |
. When you want to get the difference, use &
.
I hope this example made it clearer.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论